【问题标题】:routes with react-router-dom opens in parent container带有 react-router-dom 的路由在父容器中打开
【发布时间】:2023-03-13 18:49:01
【问题描述】:

我正在尝试使用 react-router-dom 但我不明白如何定义一个“主”容器来打开里面的所有其他组件? 在我以前做的旧版本中

  <Router history={history}>
    <Route path="/" component={Main}>
      <IndexRoute component={ShowsPage} />

      <Route path="/shows" component={ShowsPage} />
      <Route path="/tickets" component={ticketsPage} />
    </Route>

    <Redirect from="*" to="/" />
  </Router>

它会像魔术一样工作,现在我尝试了这样的事情

<Router>
    <div>
        <Route component={Main}/>
        <Route path="/" exact component={ShowsPage}/>
        <Route path="/tickets" exact component={ticketsPage}/>
        <Route path="/shows" exact component={ShowsPage}/>
    </div>
</Router>

我不确定如何实现未找到的路由,但在我最后一次尝试处理路由时,我设法让它们在容器内打开,但我也收到了这个警告The prop `children` is marked as required in `Main`, but its value is `undefined,我想知道为什么我会得到它们?我做错了什么?以及如何设置所有其他路由(除了定义的)将被重定向到“/”(到 ShowsPage 组件)

这里是 Main.js(我的主容器)

import React from 'react';
import PropTypes from 'prop-types';

import HeaderContainer from './HeaderContainer';
import FooterContainer from './FooterContainer';

const Main = ({children}) => (
    <div>
        <container className="containerHeader">
            <HeaderContainer />
        </container>
        <container className="containerMain">
            {children}
        </container>
        <container className="containerFooter">
            <FooterContainer />
        </container>
    </div>
)

Main.propTypes = {
    children: PropTypes.object.isRequired,
}


export default Main;

谢谢!

【问题讨论】:

    标签: reactjs react-router react-router-dom


    【解决方案1】:

    假设您想在Main 组件内显示ShowsPageticketsPageShowsPage。 您必须将这些组件路由放在Main 组件内

    history 属性添加到Router

    import createBrowserHistory from 'history/createBrowserHistory'
    const browserHistory = createBrowserHistory();
    
    <Router history={browserHistory}>
        <div>
            <Route path="/" component={Main} />
        </div>
    </Router>
    

    将子组件路由移动到主(父)组件内。

    const Main = ({ match }) => { //assume main component.
        return (<div>
            This is the Root component.
            <Route path={`${match.url}`} exact component={ShowsPage} ></Route>
            <Route path={`${match.url}tickets`} exact component={ticketsPage}></Route>
            <Route path={`${match.url}shows`} exact component={ShowsPage}></Route>
        </div>)
    };
    

    【讨论】:

    • 太棒了!它有效,但是我现在如何以编程方式更改路线(假设我想将路线从 ticketsPage 切换到 ShowsPage ?非常感谢!
    • 表示重定向?
    【解决方案2】:
    <Route component={Main}/>
    <Route path="/" exact component={ShowsPage}/>
    <Route path="/tickets" exact component={ticketsPage}/>
    <Route path="/shows" exact component={ShowsPage}/>
    

    应该是

    <Route component={Main} />
    

    在 Main 的渲染方法中添加路由。像这样将它们放在 Switch 中(从 react-router-dom 导入)

    <Switch>
     <Route path="/" exact component={ShowsPage}/>
     <Route path="/tickets" exact component={ticketsPage}/>
     <Route path="/shows" exact component={ShowsPage}/>
    </Switch>
    

    【讨论】:

    • 现在我收到 2 个警告,Warning: You should not use &lt;Route component&gt; and &lt;Route children&gt; in the same route; &lt;Route children&gt; will be ignoredWarning: Failed prop type: The prop `children` is marked as required in `Main`, but its value is `undefined`.
    • 尝试更新后的答案,让我知道它是否有效
    • 你是那个意思吗? pastebin.com/UNNmqHdU 如果是,我仍然会得到 Warning: Failed prop type: The prop 'children' is marked as required in 'Main', but its value is 'undefined',当我尝试将 url 更改为未定义的内容时,我只得到 Main 组件,其中没有 ShowsPage 组件。除了一切都很好(正如我出现时我第一次尝试所做的那样)。谢谢@RaviTeja Teja
    • 里面添加Switch,也不要按要求做children
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 2019-05-30
    • 2018-06-08
    • 2018-06-04
    • 1970-01-01
    相关资源
    最近更新 更多