1

I'm new to the ES6 module system so this may sound silly.

In the React Native Starter App that uses NativeBase, can anyone explain how native-starter-kit/js/components/home/index.js can do

import myTheme from '../../themes/base-theme`; 

when the file it is importing from, native-starter-kit/js/themes/base-theme.js did not have any code that export default the variable myTheme?

2 Answers 2

4

The 2nd file you are refering to is an entire export default. On line 5 :

export default { // Badge badgeBg: '#ED1727', badgeColor: '#fff', ... } 

So when they do import myTheme from '../../themes/base-theme; the myTheme is the variable name they chose for the imported module. In this case, the transpiler is going to look for the default export.

If they would have done import { myTheme } from '../../themes/base-theme; then there would have been an issue since it is not explicitly named in the export. Doing this is not refering to the default export but rather the one explicitly called myTheme

I am not sure I made sense, you may want to give https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Instructions/import a read

Sign up to request clarification or add additional context in comments.

Comments

2

Exporting without default means it's a named export.

You can have multiple named exports in a single file.

Example 1: export class Theme {}

Here you have to import this export using its exact name.

To use this component in other file you should do this, import { Theme } from '../../themes/base-theme'

Example 2:

If you export as the default export as, export default class Theme {}

Then in another file you import the default export without using the { }, like this, import Theme from '../../themes/base-theme'

There can only be one default export per file.

Although its React's convention to export one component from a file, and to export it is as the default export.

You're free to rename the default export as you import it. import myTheme from '../../themes/base-theme'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.