React Router Dom v6 - 重定向到另一个路由 [英] React Router Dom v6 - redirecting to another route

查看:15
本文介绍了React Router Dom v6 - 重定向到另一个路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下路由器配置:

I am using the following Router config:

<Routes>
  <Route path="/" element={<Home />} />
  <Route path="/information" element={<Information />} />
  <Route path="*">
    <Navigate to="/" replace={true} />
  </Route>
</Routes>

每次输入路由器无法识别的路由时,我都想导航到主路由和主组件.从 v6 开始,没有 Redirect 组件,所以我尝试使用 Navigate.

I would like to navigate to home route and Home component every time when I type the route that is not recognized by the router. Since v6, there is no Redirect component, So I am trying to use Navigate.

为什么我的配置不起作用?

推荐答案

复制 Redirect 的新语法/模式是将 Navigate 渲染为路由 element,并指定 replace 属性.

The new syntax/pattern to replicate the Redirect is to render the Navigate as the route element, and specify the replace prop.

RRDv5:<重定向自=*"to=/";/>

RRDv6: <Route path=*"element={<导航到=/";替换/>}/>

这篇关于React Router Dom v6 - 重定向到另一个路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆