【问题标题】:Nesting child routes within another child route在另一个子路由中嵌套子路由
【发布时间】:2016-08-09 21:59:46
【问题描述】:

编辑

我最初的问题包括带有分割点的路线,但我已将其简化为最简单的用例,即仅将子路线相互嵌套。

作为参考,我正在使用the popular react-redux-starter-kit,我正在尝试向我的路由添加一个简单的包装器组件,如下所示:

export const createRoutes = (store) => ({
      path: '/',
      component: CoreLayout,
      indexRoute: Home,
      childRoutes: [{
        component: TransitionWrapper,
        childRoutes: [
          CounterRoute(store)
          ]
      }]
    })

但是我收到以下错误并且我的子路由没有被渲染:

Warning: Failed prop type: Required prop `children` was not specified in `CoreLayout`.
    in CoreLayout (created by RouterContext)
    in RouterContext (created by Router)
    in Router (created by AppContainer)
    in div (created by AppContainer)
    in Provider (created by AppContainer)
    in AppContainer

因此,基本上,如果我将子路由嵌套在子路由中,我会收到有关丢失子路由的投诉。

这是完整的设置:

main.js

const MOUNT_NODE = document.getElementById('root')

let render = () => {
  const routes = require('./routes/index').default(store)

  ReactDOM.render(
    <AppContainer
      store={store}
      history={history}
      routes={routes}
    />,
    MOUNT_NODE
  )
}

/routes/index.js

import CoreLayout from '../layouts/CoreLayout/CoreLayout'
import Home from './Home'
import NestedChild from './NestedChild'
import TransitionWrapper from './TransitionWrapper'

export const createRoutes = (store) => ({
  path: '/',
  component: CoreLayout,
  indexRoute: Home,
  childRoutes: [{
    component: TransitionWrapper,
    childRoutes: [
      NestedChild
      ]
  }]
})

AppContainer.js

class AppContainer extends Component {
  static propTypes = {
    history: PropTypes.object.isRequired,
    routes: PropTypes.object.isRequired,
    store: PropTypes.object.isRequired
  }

  render () {
    const { history, routes, store } = this.props

    return (
      <Provider store={store}>
        <div style={{ height: '100%' }}>
          <Router history={history} children={routes} />
        </div>
      </Provider>
    )
  }
}

export default AppContainer

CoreLayout.js

import React from 'react'
import Header from '../../components/Header'
import classes from './CoreLayout.scss'
import '../../styles/core.scss'

export const CoreLayout = ({ children }) => (
  <div className='container text-center'>
    <Header />
    <div className={classes.mainContainer}>
      {children}
    </div>
  </div>
)

CoreLayout.propTypes = {
  children: React.PropTypes.element.isRequired
}

export default CoreLayout

TransitionWrappper.js

const TransitionWrapper = (props) => (

  <div className="im-not-working">
    {this.props.children}

  </div>
)

export default TransitionWrapper

NestedChild.js

【问题讨论】:

  • 请尝试使用“导出默认组件”可能会解决您的问题。

标签: javascript reactjs react-router


【解决方案1】:

您是否尝试过从 CoreLayoutchildren 属性中删除 isRequired

如果您是动态加载子组件,那么在您将子组件放入其中之前,CoreLayout 会呈现一段时间。

【讨论】:

  • 当您尝试这样做时,哪个组件给您带来了错误?因为 CoreLayout 不会再给出错误了...
  • 所以经过几次检查我很确定不是它,你可以看到我最新的编辑。
  • 重点当然不是对 props 的抱怨,而是子路由没有被渲染的事实。
猜你喜欢
  • 2023-01-02
  • 2019-01-03
  • 2019-01-12
  • 1970-01-01
  • 1970-01-01
  • 2022-07-24
  • 1970-01-01
  • 1970-01-01
  • 2020-09-23
相关资源
最近更新 更多