網站首頁 編程語言 正文
React Router V6 變更介紹
之前一直在用5.x版本的Router,突然發現Router V6有一些變化,可以說是對嵌套路由更加友好了。這里,我們就做個簡單的介紹。
1. < Switch > 重命名為< Routes >
之前在用Router時,需要用Switch包裹,Switch可以提高路由匹配效率(單一匹配) 。在V6中,該頂級組件將被重命名為Routes,注意的是其功能大部分保持不變。
2. < Route >的新特性變更
component/render被element替代
// v5 <Switch> <Route path="/about" component={About}/> <Route path="/home" component={Home}/> </Switch> //v6 <Routes> <Route path="/about" element={<About/>}/> <Route path="/home" element={<Home/>}/> </Routes>
3. 嵌套路由變得更簡單
3.1 具體變化有以下:
- < Route children > 已更改為接受子路由。
- 比< Route exact >和< Route strict >更簡單的匹配規則。
- < Route path > 路徑層次更清晰。
function App() { return ( <BrowserRouter> <Routes> <Route path="/" element={<Home />} /> <Route path="/about" element={<About/>}> <Route path="/about/message" element={<Message/>} /> <Route path="/about/news" element={<News/>} /> </Route> </Routes> </BrowserRouter> ); } function About() { return ( <div> <h1>About</h1> <Link to="/about/message">Message</Link> <Link to="/about/news">News</Link> {/* 將直接根據上面定義的不同路由參數,渲染<MyProfile />或<OthersProfile /> */} <Outlet /> </div> ) }
這里的< Outlet /> 相當于占位符,像極了{this.props.children},也越來越像Vue了?。
3.2 廢棄了V5中的Redirect
//v5 <Redirect to="/"/> //v6 <Route path="*" element={<Navigate to="/" />}/>
3.3 多個< Routes />
以前,我們只能 在React App中使用一個 Routes。但是現在我們可以在React App中使用多個路由,這將幫助我們基于不同的路由管理多個應用程序邏輯。
import React from 'react'; import { Routes, Route } from 'react-router-dom'; function Dashboard() { return ( <div> <p>Look, more routes!</p> <Routes> <Route path="/" element={<DashboardGraphs />} /> <Route path="invoices" element={<InvoiceList />} /> </Routes> </div> ); } function App() { return ( <Routes> <Route path="/" element={<Home />} /> <Route path="dashboard/*" element={<Dashboard />} /> </Routes> ); }
4. 用useNavigate代替useHistory
// v5 import { useHistory } from 'react-router-dom'; function MyButton() { let history = useHistory(); function handleClick() { history.push('/home'); }; return <button onClick={handleClick}>Submit</button>; }; //v6中history.push()替換為navigation() import { useNavigate } from 'react-router-dom'; function MyButton() { let navigate = useNavigate(); function handleClick() { navigate('/home'); }; return <button onClick={handleClick}>Submit</button>; };
history的用法也將被替換成:
// v5 history.push('/home'); history.replace('/home'); // v6 navigate('/home'); navigate('/home', {replace: true});
5. Hooks中新鉤子useRoutes代替react-router-config
function App() { let element = useRoutes([ { path: '/', element: <Home /> }, { path: 'dashboard', element: <Dashboard /> }, { path: 'invoices', element: <Invoices />, children: [ { path: ':id', element: <Invoice /> }, { path: 'sent', element: <SentInvoices /> } ] }, // 重定向 { path: 'home', redirectTo: '/' }, // 404找不到 { path: '*', element: <NotFound /> } ]); return element; }
總結
原文鏈接:https://blog.csdn.net/weixin_47809584/article/details/122096676
相關推薦
- 2022-08-13 Redis - String內存開銷問題以及基本/擴展數據類型的使用
- 2024-02-17 序列轉序模型及其pytorch實現
- 2022-07-13 Docker資源限制Cgroup
- 2023-12-26 layui彈窗傳值
- 2022-08-15 Property or field ‘xxx‘ cannot be found on object
- 2022-11-25 Python執行dos和Linux命令的方法詳解_python
- 2022-09-16 詳解SQL報錯盲注_MsSql
- 2022-08-16 Hive導入csv文件示例_數據庫其它
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支