react-router4 map循环出的可以切换路由地址,但是切换不了组件

发布于 2022-09-07 21:43:49 字数 902 浏览 28 评论 0

<ul className="nav">
    <li>
        <Link to="/section1">test1</Link>
    </li>
    <li>
        <Link to="/section2">test2</Link>
    </li>
    <li>
        <Link to="/section3">test3</Link>
    </li>
    //上边直接写正常
    ////////////////////////////////////////
    //下边就不行,地址可以切换,组件出不来,也切换不了
    {list.map((item, index) => {
        return <li key={index}>
            <Router>
                <Link to={`/section${index + 1}`}>{item}</Link>
            </Router>
            </li>
        })}
</ul>

{this.props.children}
<Switch>
    <Route path="/section1" component={Section1}/>1
    <Route path="/section2" component={Section2}/>2
    <Route path="/section3" component={Section3}/>3
</Switch>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那些过往 2022-09-14 21:43:49
{list.map((item, index) => {
    return <li key={index}> 
            <Link to={`/section${index + 1}`}>{item}</Link>
        </li>
    })}

因为我嵌套了2层<Router>去掉就可以了...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文