如何在不在 react-router-dom 中嵌套 url 的情况下重定向页面

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')

您可以使用 Redirect react-router-dom

提供的组件

例如:

import {Redirect} from 'react-router-dom'
function MyComponent (){
  return (
    <>
      {condition && <Redirect to='/your-desire-path'/>}
      <MyOtherStaff/>
    </>
  )
}