【问题标题】:React router throws and error when getting async component获取异步组件时反应路由器抛出和错误
【发布时间】:2016-03-11 17:01:25
【问题描述】:

我正在使用 react、redux 和 react router 来构建和示例应用程序。

我正在尝试异步加载应用程序的不同部分。我已将我的应用程序划分为鸭子,我正在遵循这个示例https://github.com/insin/react-examples/tree/master/code-splitting-redux-reducers

但是我收到了这个错误:

Uncaught Invariant Violation: The root route must render a single element

尝试使用 react 路由器的 getComponent 方法获取异步组件时。 我正在使用: 反应路由器 2.0.1

我的路线:

export default function configureRoutes(reducerRegistry) {
  return(
    <Route>
      <Route component={Landing}>
        <Route path='/login' component={Login}/>
        <Route path='/register' component={Register}/>
      </Route>
            <Route path="admin" getComponent={(location, cb) => {
                require.ensure([], require => {
                    cb(null, require('./containers/admin'))
                })
            }}/>
      <Route component={App}>
        <Route  path='/' component={Home} />
      </Route>
    </Route>
)}

我的组件

class Admin extends Component {
  componentDidMount() {
    this.props.load()  
  }
  render() {
    const { message, isFetching } = this.props
    return (
      <div>
        <p>{message}</p> 
        <p>This module was loaded via chunk </p>
        {loading && <p>Doing some fake loading ...</p>}
      </div>
    )  
  }  
}

Admin.propTypes = {
  message: PropTypes.string.isRequired,
  isFetching: PropTypes.bool.isRequired,
  load: PropTypes.string.isRequired
}

const mapStateToProps = state => state.admin

function mapDispatchToProps(dispatch) {
  return bindActionCreators({ load }, dispatch)
}

export default connect(mapStateToProps, mapDispatchToProps)(Admin)

有人有同样的错误吗?有任何想法吗?有人有类似的工作吗?

感谢社区!

更新:为清晰起见添加了 index.js

import configureRoutes from './routes'
import configureStore from './store/configureStore'
import coreReducers from './modules/core'
import ReducerRegistry from './reducer-registry'

var reducerRegistry = new ReducerRegistry(coreReducers)

// Configure hot module replacement for core reducers
if (process.env.NODE_ENV !== 'production') {
  if (module.hot) {
    module.hot.accept('./modules/core', () => {
      var nextCoreReducers = require('./modules/core')
      reducerRegistry.register(nextCoreReducers)
    })
  }
}

const routes = configureRoutes(reducerRegistry)
const store = configureStore(reducerRegistry)

render(
  <I18nextProvider i18n={i18n}>
    <Provider store={store}>
      <Router history={browserHistory} routes={routes} />
    </Provider>
  </I18nextProvider>,
  document.getElementById('root')
)

【问题讨论】:

  • 错误非常具体,与异步部分无关。请查看您所遵循示例中的路由配置。请注意所有Routes 都有路径吗? github.com/insin/react-examples/blob/master/…
  • 你的根路径/ 必须是根,实际上..
  • 我在 dev webpack 1.12.13 webpack-dev-middleware 1.5.1 webpack-hot-middleware 2.6.4 上使用的 Webpack 版本
  • 你的路由配置不正确,和webpack无关。
  • @azium 我不知道我是否明白你在说什么。我的路线也有一个指向管理员的路径(我错过了“/”,但如果我添加它会引发同样的错误)

标签: react-router redux


【解决方案1】:

我认为您的根 &lt;Route&gt; 缺少 component 字段。

您需要为每个父路由指定componentgetComponent,因为这将是当前子路由的组件作为this.props.children 传递给的组件。

而不是

export default function configureRoutes(reducerRegistry) {
  return (
    <Route>

你想要类似的东西

export default function configureRoutes(reducerRegistry) {
  return (
    <Route component={App}>

在这种情况下,您可能不需要下面的另一个App 路由。

【讨论】:

  • 谢谢丹。我设法解决了这个错误,将admin 路由移动到Landing 路由内,所以我知道每条使用getComponent 的路由都应该有一个带有组件的父路由,正如你所说,传递给这样.props.children。但是我仍然有一个没有组件的根route,以便包装两个没有共同点的不同布局。你认为这是错误的做法吗?
  • 现在 admin 的父路由的 this.props.childrenundefined,而不是请求异步组件。我认为这不相关,所以我会提出一个新问题。
  • 我已经修复了另一个错误。如果有人感兴趣,请联系here
猜你喜欢
  • 2016-06-28
  • 2017-08-05
  • 2016-05-08
  • 1970-01-01
  • 1970-01-01
  • 2017-12-15
  • 2019-09-17
  • 2020-02-05
  • 2021-03-01
相关资源
最近更新 更多