274

This component does work:

export class Template extends React.Component {
    render() {
        return (
            <div> component </div>
        );
    }
};
export default Template;

If i remove last row, it doesn't work.

Uncaught TypeError: Cannot read property 'toUpperCase' of undefined

I guess, I don't understand something in es6 syntax. Isn't it have to export without sign "default"?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
stkvtflw
  • 12,092
  • 26
  • 78
  • 155

3 Answers3

636

Exporting without default means it's a "named export". You can have multiple named exports in a single file. So if you do this,

class Template {}
class AnotherTemplate {}

export { Template, AnotherTemplate }

then you have to import these exports using their exact names. So to use these components in another file you'd have to do,

import {Template, AnotherTemplate} from './components/templates'

Alternatively if you export as the default export like this,

export default class Template {}

Then in another file you import the default export without using the {}, like this,

import Template from './components/templates'

There can only be one default export per file. In React it's a 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 TheTemplate from './components/templates'

And you can import default and named exports at the same time,

import Template,{AnotherTemplate} from './components/templates'
Jed Richards
  • 12,244
  • 3
  • 24
  • 35
  • 14
    OK. But this seems like yet another seemingly arbitrary decision that I don't see the rationale for but have to memorize. Am I missing some good reason it is like this? In many a project there will be dozens of React components. Having each its own file, no matter how small seems, well, a bit anal. It particular gets painful if many of them share clumps of helper functions. It makes for more lines of stuff to keep in sync which seems a bit counter-goodness. What am I missing? –  Jun 06 '16 at 09:03
  • 2
    All stylistic decisions and conventions like this are somewhat arbitrary and down to personal preference. You're right, sometimes it might make sense to group many components into one file with helpers, especially for small/simple projects. I guess one reason to have one component per file is testing ... when importing that file to test it, it can help if you're just importing the minimal amount of other code and sub-dependencies along with it. Another reason could be for simple diffs on small focussed files during code reviews etc. – Jed Richards Jun 06 '16 at 12:29
  • 10
    Good answer. I have something to add to it: Try to use imports statements like this: `import RaisedButton from 'material-ui/RaisedButton';` instead of `import {RaisedButton} from 'material-ui';` This will make your build process faster and your build output smaller. – Shekhar Kumar Oct 05 '16 at 22:12
  • 3
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export – xgqfrms Feb 06 '17 at 11:50
  • 5
    @ShekharKumar Do you have a source for `import Binding from 'module/Binding'` being more efficient than `import {Binding} from 'module'`? – Jeevan Takhar Mar 28 '18 at 15:15
  • what about the case where you want to be able to both of these things: import colors from './colors' and import {red, green,yellow,blue} from './colors' ? Do you have to define the vars twice? once as an object and then a second time an individually exported constants? – Daniel Lefebvre May 07 '19 at 21:02
  • 1
    @DanielLefebvre check my last example, you can import named imports and the default import at the same time. e.g. `import colors, {red, green, blue} from './colors'` – Jed Richards May 08 '19 at 10:49
  • How can one pass ```props``` through a named export? React does not seem to understand this syntax when passing props. I wanna export several styled components with their respective props along. – Arp Sep 14 '20 at 21:13
  • @Alioshr I think something else is going wrong in your case, there's no difference from React or Styled Components point of view in terms of default or named exports. Once those are imported, they are the same JavaScript object. – Jed Richards Sep 15 '20 at 15:46
  • I am using styled components with SSR, in this case, namely, NextJs. I still very new to this framework. The problem was that I needed to workaround the SSR rendering with async code to manage to receive the props in a timely manner. Problem solved – Arp Sep 15 '20 at 21:09
15

Add { } while importing and exporting: export { ... }; | import { ... } from './Template';

exportimport { ... } from './Template'

export defaultimport ... from './Template'


Here is a working example:

// ExportExample.js
import React from "react";

function DefaultExport() {
  return "This is the default export";
}

function Export1() {
  return "Export without default 1";
}

function Export2() {
  return "Export without default 2";
}

export default DefaultExport;
export { Export1, Export2 };

// App.js
import React from "react";
import DefaultExport, { Export1, Export2 } from "./ExportExample";

export default function App() {
  return (
    <>
      <strong>
        <DefaultExport />
      </strong>
      <br />
      <Export1 />
      <br />
      <Export2 />
    </>
  );
}

⚡️Working sandbox to play around: https://codesandbox.io/s/export-import-example-react-jl839?fontsize=14&hidenavigation=1&theme=dark

Hasan Sefa Ozalp
  • 6,353
  • 5
  • 34
  • 45
1
    // imports
    // ex. importing a single named export
    import { MyComponent } from "./MyComponent";
// ex. importing multiple named exports
    import { MyComponent, MyComponent2 } from "./MyComponent";
// ex. giving a named import a different name by using "as":
    import { MyComponent2 as MyNewComponent } from "./MyComponent";
// exports from ./MyComponent.js file
    export const MyComponent = () => {}
    export const MyComponent2 = () => {}
    
    import * as MainComponents from "./MyComponent";
    // use MainComponents.MyComponent and MainComponents.MyComponent2
    //here

EXPORTING OBJECT:

class EmployeeService { }
export default new EmployeeService()

import EmployeeService from "../services/EmployeeService"; // default import

EXPORTING ARRAY

 export const arrExport = [
        ['first', 'First'],
        ['second', 'Second'],
        ['third', 'Third'],
      ]
    
    import {arrExport} from './Message' //named import

// if not react and javascript app then mention .js extension in the import statement.

You can export only one default component and in import can change the name without aliasing it(using as).

subhashis
  • 4,629
  • 8
  • 37
  • 52