exporting multiple modules in react.js

前端 未结 2 1760
半阙折子戏
半阙折子戏 2020-12-13 03:49

New to react.js and trying to following tutorial. Unfortunately the code given in the page didn\'t work. webpack complained

ERROR in ./App.jsx
Module build         


        
2条回答
  •  长情又很酷
    2020-12-13 04:22

    You can have only one default export which you declare like:

    export default App; or export default class App extends React.Component {...

    and later do import App from './App'

    If you want to export something more you can use named exports which you declare without default keyword like:

    export {
      About,
      Contact,
    }
    

    or:

    export About;
    export Contact;
    

    or:

    export const About = class About extends React.Component {....
    export const Contact = () => (
    ...
    );

    and later you import them like:

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

    EDIT:

    There is a mistake in the tutorial as it is not possible to make 3 default exports in the same main.js file. Other than that why export anything if it is no used outside the file?. Correct main.js :

    import React from 'react';
    import ReactDOM from 'react-dom';
    import { Router, Route, Link, browserHistory, IndexRoute  } from 'react-router'
    
    class App extends React.Component {
    ...
    }
    
    class Home extends React.Component {
    ...
    }
    
    
    class About extends React.Component {
    ...
    }
    
    
    class Contact extends React.Component {
    ...
    }
    
    
    ReactDOM.render((
       
          
             
             
             
             
          
       
    
    ), document.getElementById('app'))
    

    EDIT2:

    another thing is that this tutorial is based on react-router-V3 which has different api than v4.

提交回复
热议问题