ReactJS Error : Only one default export allowed per module

谁说胖子不能爱 提交于 2019-12-04 02:39:32

Multiple component does work but you need to export the component with name and you can only export one default component.

Like in below expample I export the App Component as defalut component and other component Home, About, Contact as a named component.

For the named component you need to import them using there name :

import {Home,About,Contact} from './App.jsx';

import default component:

import App from './App.jsx';

import React from 'react';
import {Link} from 'react-router';


class App extends React.Component {
  render() {
    return(
      <div>
      <ul>
      <li><Link to="home">Home</Link></li>
      <li><Link to="about">About</Link></li>
      <li><Link to="contact">Contact</Link></li>
      </ul>

      {this.props.children}
      </div>
    )
  }
}

export default App;



export class Home extends React.Component {
  render() {
    return (
      <h1>Home Page Content</h1>
    )
  }
}



export class About extends React.Component {
  render() {
    return (
      <h1>About Page Content</h1>
    )
  }
}


export class Contact extends React.Component {
  render()  {
    return (
      <h1>Contact Page Content</h1>
    )
  }
}

import React from 'react';
import ReactDOM from 'react-dom';
import {Router, Route, browserHistory, IndexRoute} from 'react-router';
import App from './App.jsx';
import {Home,About,Contact} from './App.jsx';

ReactDOM.render((
   <Router history = {browserHistory}>
      <Route path = "/" component = {App}>
         <IndexRoute component = {Contact} />
         <Route path = "home" component = {Home} />
         <Route path = "about" component = {About} />
         <Route path = "contact" component = {Contact} />
      </Route>
   </Router>

	
), document.getElementById('app'));

Don't import default component (App Component) with the name component (Home, About, Contact). if you import them with the named component they didn't render properly.

Blockquote

import React from 'react';
import {Link} from 'react-router';


class App extends React.Component {
  render() {
    return(
      <div>
      <ul>
      <li><Link to="home">Home</Link></li>
      <li><Link to="about">About</Link></li>
      <li><Link to="contact">Contact</Link></li>
      </ul>

      {this.props.children}
      </div>
    )
  }
}

export default App;



export class Home extends React.Component {
  render() {
    return (
      <h1>Home Page Content</h1>
    )
  }
}



export class About extends React.Component {
  render() {
    return (
      <h1>About Page Content</h1>
    )
  }
}


export class Contact extends React.Component {
  render()  {
    return (
      <h1>Contact Page Content</h1>
    )
  }
}

you have one line with the code:

export default App;

After some other lines you have the code:

export default Home;

That's just the problem! you have used export default 2 times in one file. you have to change one of them to solve the problem.

"you cannot use export default more than one time in a file".

export all the components in one line

export default {App, Home, Contacts, About};

You need to remove default keywords on both App and Home classes, as per code below:

export App;
export Home;

default keywords is only use when you want to export ONE class.

Muhammad Talha

Export Default Home is used to Expose any module to use in other files, but only one component is a default not all. A module can only be exported once. You are using the same statement to export each component which is unnecessary.

Importing components using this statement

import {Home,About,Contact} from './App.jsx';

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!