简体   繁体   English

反应路由器嵌套路由不起作用

[英]React router nested routing is not working

I am using react router v6 and trying to nest routers to switch only a particular div of the page.我正在使用 react router v6 并尝试嵌套路由器以仅切换页面的特定 div。

I tried doing this我试过这样做

<div className="user-profile-page">
    <div className="user-profile-page-background">
        <img className="full-img" src = {user.profile_picture_url} />
    </div>
    <div className = "user-profile-contents-container">
        <h1 className="user-profile-name ellipsis">{user.full_name} ({user.age})</h1>
        <div className = "user-profile-nav">
            <NavLink to = "" className={(state)=>!state.isActive?"user-profile-nav-item":"user-profile-nav-item user-profile-nav-item-active"}>
                About
            </NavLink>
            <NavLink to = "pictures" className={(state)=>!state.isActive?"user-profile-nav-item":"user-profile-nav-item user-profile-nav-item-active"}>
                Pictures
            </NavLink>
        </div>
        <div className = "user-profile-contents-main">
            <Routes>
                <Route index={true} element = {<UserProfileAbout />} />
                <Route path = "pictures" element =  {<UserProfilePictures />} />
            </Routes>
        </div>
    </div>
</div>

but when I go to "/user/pictures" instead of just changing the contents inside "user-profile-contents-main" div .但是当我 go 到"/user/pictures"而不是仅仅更改"user-profile-contents-main" div中的内容时。 It changes the whole page.它改变了整个页面。

Here is my main router这是我的主路由器

<Routes>
    <Route path = "/" element = {<HomePage /> } />
    <Route path = "/message/:uid" element = {<ChatPage />} />
    <Route path = "/user/:uid" element = {<UserProfilePage />} />
</Routes>

EDIT编辑

all i had to do was我所要做的就是

<Routes>
    <Route path = "/" element = {<HomePage /> } />
    <Route path = "/message/:uid" element = {<ChatPage />} />
    <Route path = "/user/:uid/*" element = {<UserProfilePage />} />
</Routes>

add /* in the route path在路由路径中添加/*

 <Routes>
    <Route index={true} element = {<UserProfileAbout />} />
    <Route path = "pictures" element =  {<UserProfilePictures />} />
 </Routes>

no need to declare a new navigation inside the component in which it should be called, if you want to isolate some screens it is better to create a separate stack for them不需要在应该调用它的组件内声明一个新的导航,如果你想隔离一些屏幕,最好为它们创建一个单独的堆栈

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM