[英]How to redirect page without nesting the url in react-router-dom
我在我的應用程序中使用 react-router-dom v5 進行路由。 在某些情況下,我有這樣的路線:
checkup/step-1/:id
checkup/step-2/:id
checkup/step-3/:id
例如,我在checkup/step-1/:id
所以我想從那里重定向到checkup/step-2/:id
。 要重定向,我使用hisory.push()
。 但是結果變成了這樣:
checkup/step-1/:id/checkup/step-2/:id
但我的預期結果是:
checkup/step-2/:id
在開頭使用/
。
例如:
history.push('/checkup/step-2/:id')
代替
history.push('checkup/step-2/:id')
您可以使用 react-router-dom 提供的Redirect
組件
例如:
import {Redirect} from 'react-router-dom'
function MyComponent (){
return (
<>
{condition && <Redirect to='/your-desire-path'/>}
<MyOtherStaff/>
</>
)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.