react-router-dom从V5升级到V6后,有些使用做了一些改变:
(1) Switch 重命名为 Routes
// v5
<Switch>
<Route exact path="/"><Home /></Route>
<Route path="/profile"><Profile /></Route>
</Switch>
// v6
<Routes>
<Route path="/" element={<Home />} />
<Route path="profile/*" element={<Profile />} />
</Routes>
(2) Route 的新特性变更 ,component/render被element替代
import Profile from './Profile';
// v5
<Route path=":userId" component={Profile} />
<Route
path=":userId"
render={routeProps => (
<Profile routeProps={routeProps} animate={true} />
)}
/>
// v6
<Route path=":userId" element={<Profile />} />
<Route path=":userId" element={<Profile animate={true} />} />
(3) 嵌套路由变得更简单
具体变化有以下:
- Route children 已更改为接受子路由。
- 比Route exact 和 Route strict更简单的匹配规则。
- Route path 路径层次更清晰。