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

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


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>      );    }}

by using default you express that's going to be member in that module which would be imported if no specific member name is provided. You could also express you want to import the specific member called MyNavbar by doing so: import {MyNavbar} from './comp/my-navbar.jsx'; in this case, no default is needed


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';


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'