【问题标题】:React Router - dynamic routing setupReact Router - 动态路由设置
【发布时间】:2016-12-03 00:23:49
【问题描述】:

我确定我在这里遗漏了一些简单的东西,但无法正确配置我的路线。

我得到这个错误:

Warning: Failed prop type: The prop 'children' is marked as required in 'App', but its value is 'undefined'. in App (created by RouterContext) in RouterContext (created by Router) in Router

这是我的基本设置:

./index.jsx

import React from 'react';
import ReactDOM from 'react-dom';
import {Router, browserHistory} from 'react-router';
import routes from './routes';

ReactDOM.render(
  <Router history={browserHistory} routes={routes}/>, document.getElementById('root'));

./routes/index.js

module.exports = {
  path: '/',
  component: require('../components/app'),
  indexRoute: require('./home')
};

./routes/home.js

module.exports = {
  getComponent(nextState, cb) {
    require.ensure([], (require) => {
      cb(null, require('../components/home'));
    });
  }
};

./components/app/index.jsx

import React, {PropTypes} from 'react';

const App = props => (
  <section>
    <h1>My App</h1>
    <section>{props.children}</section>
  </section>
);

App.propTypes = {
  children: PropTypes.node.isRequired
};

export default App;

./components/home/index.jsx

import React from 'react';

const Home = () => (
  <section>
    <h3>Home</h3>
  </section>
);

export default Home;

【问题讨论】:

    标签: reactjs react-router react-routing


    【解决方案1】:

    需要使用导出的文件时需要指定.default。没有它 Home 将是未定义的,因此会出现错误

    ./routes/home.js

    module.exports = {
        getComponent(nextState, cb) {
            require.ensure([], (require) => {
    
            // need to specify .default when requiring a file using export
            cb(null, require('../components/home').default);
            });
        }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-29
      • 2022-08-14
      • 1970-01-01
      • 2016-11-14
      • 1970-01-01
      • 2016-07-09
      • 2021-09-26
      相关资源
      最近更新 更多