react-router-dom v6版本中的withRouter和Switch已過時,可以退回到v5版本繼續使用,或者使用useNavigate()替代withRouter,使用Routes替代Switch。
例如:
const navigate = useNavigate()
navigate('/test') // 跳轉到/test
navigate(-1) // 返回上一級
navigate(0, {replace: true})// 強制刷新當前頁面並不加入路由歷史
和
<Routes>
<Route path='/xxx' element={<Xxx/>} />
......
</Routes>
注意之前Route組件中的是component={Xxx},改為了element={
最后貼一個react-router-dom v6的api地址
https://reactrouter.com/docs/en/v6/api