網(wǎng)站首頁 編程語言 正文
React Router V6 變更介紹
之前一直在用5.x版本的Router,突然發(fā)現(xiàn)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 >更簡單的匹配規(guī)則。
- < 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> {/* 將直接根據(jù)上面定義的不同路由參數(shù),渲染<MyProfile />或<OthersProfile /> */} <Outlet /> </div> ) }
這里的< Outlet /> 相當(dāng)于占位符,像極了{(lán)this.props.children},也越來越像Vue了?。
3.2 廢棄了V5中的Redirect
//v5 <Redirect to="/"/> //v6 <Route path="*" element={<Navigate to="/" />}/>
3.3 多個< Routes />
以前,我們只能 在React App中使用一個 Routes。但是現(xiàn)在我們可以在React App中使用多個路由,這將幫助我們基于不同的路由管理多個應(yīng)用程序邏輯。
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; }
總結(jié)
原文鏈接:https://blog.csdn.net/weixin_47809584/article/details/122096676
相關(guān)推薦
- 2022-12-29 python中的多進(jìn)程的創(chuàng)建與啟動方式_python
- 2023-01-02 最新C語言中g(shù)etchar的使用_C 語言
- 2022-12-07 C++中new的用法及說明_C 語言
- 2022-07-09 python?查看cpu的核數(shù)實現(xiàn)_python
- 2023-04-03 python之語句mode?=?'test'?if?y?is?None?else?'train'問題
- 2022-06-16 nginx限流及配置管理實戰(zhàn)記錄_nginx
- 2022-08-14 協(xié)同開發(fā)巧用gitignore中間件避免網(wǎng)絡(luò)請求攜帶登錄信息_Golang
- 2022-03-20 Android四大組件之Activity詳細(xì)介紹_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支