reactjs - How to make redirect in nested routes with React Router
问题描述
After signin process my app redirects to /admin/dashboard , and when entering dashboard path i want my app to load first component and redirect to /admin/dashboard/profiles. In App.js component i have code
<Router>
<Layout>
<Navbar />
<Switch>
...
<Route exact path="/admin/dashboard" component={Dashboard}/>
...
</Switch>
</Layout>
</Router>
In component i have code
<Layout>
<Sidebar />
<Switch>
<PrivateRoute path={`${match.url}/profiles`} component={Profiles} />
<PrivateRoute path={`${match.url}/tests`} component={Tests} />
<PrivateRoute path={`${match.url}/settings`} component={Settings} />
</Switch>
</Layout>
It only loads component when the path of first component is path={${match.url}/
} , but when path is /admin/dashboard/profiles it does not render Profiles component. What am i doing wrong ?
解决方案
You are using an exact attribute on the top level route
<Route exact path="/admin/dashboard" component={Dashboard}/>
Which is why any nested route won't match. Remvove the exact attribute from the parent route and it would work for you
<Route path="/admin/dashboard" component={Dashboard}/>
Also for paths, you must use match.path
instead of match.url
. match.url
should be used for Link
and Redirect
component
推荐阅读
- r - 在数据框中移动整行
- c# - (C#) 垂直对齐 CheckedListBox 项的文本
- python - 使用 SQLAlchemyAutoSchema 访问烧瓶棉花糖模式类中的模型道具
- python - 字符串中的数据在 python 中变得不可用
- python - 如何对数据集中的所有值产生相同数量的噪声?
- linux - 仅测试VM中内存负载的方法是什么
- nsis - NSIS - 另一种为静默模式调用页面回调函数的方法
- docker - Symfony 与 DDEV CLI 数据库连接
- performance - 重新加载时请求阻塞/等待
- python - 给定一个向量和两者之间的角度,找到第二个 n 维向量