Home » Reactjs » How to import and export components using React + ES6 + webpack?

How to import and export components using React + ES6 + webpack?

Posted by: admin November 30, 2017 Leave a comment

Questions:

I’m playing around with React and ES6 using babel and webpack. I want to build several components in different files, import in a single file and bundle them up with webpack

Let’s say I have a few components like this:

my-navbar.jsx

import React from 'react';
import Navbar from 'react-bootstrap/lib/Navbar';

export class MyNavbar extends React.Component {
    render(){
      return (
        <Navbar className="navbar-dark" fluid>
        ...
        </Navbar>
      );
    }
}

main-page.jsx

import React from 'react';
import ReactDOM from 'react-dom';

import MyNavbar from './comp/my-navbar.jsx';

export class MyPage extends React.Component{
  render(){
    return(
        <MyNavbar />
        ...
    );
  }
}

ReactDOM.render(
  <MyPage />,
  document.getElementById('container')
);

Using webpack and following their tutorial, I have main.js:

import MyPage from './main-page.jsx';

After building the project and running it, I get the following error in my browser console:

Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. Check the render method of `MyPage`.

What am I doing wrong? How can I properly import and export my components?

Answers:

Try defaulting the exports in your components:

import React from 'react';
import Navbar from 'react-bootstrap/lib/Navbar';

export default class MyNavbar extends React.Component {
    render(){
      return (
        <Navbar className="navbar-dark" fluid>
        ...
        </Navbar>
      );
    }
}

Questions:
Answers:

Wrapping components with braces if no default exports:

import {MyNavbar} from './comp/my-navbar.jsx';

or import multiple components from single module file

import {MyNavbar1, MyNavbar2} from './module';

Questions:
Answers:

To export a single component in ES6, you can use export default as follows:

class MyClass extends Component {
 ...
}

export default MyClass;

And now you use the following syntax to import that module:

import MyClass from './MyClass.react'

If you are looking to export multiple components from a single file the declaration would look something like this:

export class MyClass1 extends Component {
 ...
}

export class MyClass2 extends Component {
 ...
}

And now you can use the following syntax to import those files:

import {MyClass1, MyClass2} from './MyClass.react'

Questions:
Answers:

MDN has really nice documentation for all the new ways to import and export modules is ES 6 Import-MDN . A brief description of it in regards to your question you could’ve either:

  1. Declared the component you were exporting as the ‘default’ component that this module was exporting:
    export default class MyNavbar extends React.Component { , and so when Importing your ‘MyNavbar’ you DON’T have to put curly braces around it : import MyNavbar from './comp/my-navbar.jsx';.
    Not putting curly braces around an import though is telling the document that this component was declared as an ‘export default’. If it wasn’t you’ll get an error (as you did).

  2. If you didn’t want to declare your ‘MyNavbar’ as a default export when exporting it : export class MyNavbar extends React.Component { , then you would have to wrap your import of ‘MyNavbar in curly braces:
    import {MyNavbar} from './comp/my-navbar.jsx';

I think that since you only had one component in your ‘./comp/my-navbar.jsx’ file it’s cool to make it the default export. If you’d had more components like MyNavbar1, MyNavbar2, MyNavbar3 then I wouldn’t make either or them a default and to import selected components of a module when the module hasn’t declared any one thing a default you can use: import {foo, bar} from "my-module"; where foo and bar are multiple members of your module.

Definitely read the MDN doc it has good examples for the syntax. Hopefully this helps with a little more of an explanation for anyone that’s looking to toy with ES 6 and component import/exports in React.

Leave a Reply

Your email address will not be published. Required fields are marked *