React Router V6 ChangesI had been using Router version 5.x, and suddenly found that Router V6 had some changes, which can be said to be more friendly to nested routes. Here, we will give a brief introduction. 1. < Switch > renamed to < Routes >Previously, when using Router, it was necessary to wrap it with Switch, which can improve the efficiency of route matching (single matching). In V6, this top-level component will be renamed to Routes, but note that its functionality remains largely unchanged. 2. New feature changes of <Route>Component/render is replaced by 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. Nested Routes Made Easier3.1 The specific changes are as follows:
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> {/* Will directly render <MyProfile /> or <OthersProfile /> according to the different routing parameters defined above */} <Outlet /> </div> ) } The < Outlet /> here is equivalent to a placeholder, which is very similar to {this.props.children}, and it is becoming more and more like Vue 😎. 3.2 Deprecated Redirect in V5//v5 <Redirect to="/"/> //v6 <Route path="*" element={<Navigate to="/" />}/> 3.3 Multiple < Routes />Previously, we could only use one Routes in React App. But now we can use multiple routes in React App which will help us to manage multiple application logic based on different routes. 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. Use useNavigate instead of useHistory// v5 import { useHistory } from 'react-router-dom'; function MyButton() { let history = useHistory(); function handleClick() { history.push('/home'); }; return <button onClick={handleClick}>Submit</button>; }; //In v6, history.push() is replaced by navigation() import { useNavigate } from 'react-router-dom'; function MyButton() { let navigate = useNavigate(); function handleClick() { navigate('/home'); }; return <button onClick={handleClick}>Submit</button>; }; The usage of history will also be replaced by: // v5 history.push('/home'); history.replace('/home'); // v6 navigate('/home'); navigate('/home', {replace: true}); 5. New hook useRoutes in Hooks replaces react-router-configfunction App() { let element = useRoutes([ { path: '/', element: <Home /> }, { path: 'dashboard', element: <Dashboard /> }, { path: 'invoices', element: <Invoices />, children: [ { path: ':id', element: <Invoice /> }, { path: 'sent', element: <SentInvoices /> } ] }, // Redirect { path: 'home', redirectTo: '/' }, // 404 Not Found { path: '*', element: <NotFound /> } ]); return element; } SummarizeThis article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: How to disable web page styles using Firefox's web developer
>>: Details about the like operator in MySQL
Introduction to vi/vim They are both multi-mode e...
Table of contents 1. Prototype Relationship 2. Pr...
Preface According to the project needs, Vue-touch...
Encapsulate a navigation bar component in Vue3, a...
This article is a self-written imitation of the X...
The Drag and Drop API adds draggable elements to ...
Copy code The code is as follows: <div content...
Mysql 8.0 installation problems and password rese...
As a backend programmer, sometimes I have to tink...
Before configuration, we need to do the following...
Perform the following operations in the Ubuntu co...
With the right settings, you can force Linux user...
There are many form elements. Here is a brief sum...
ins and del were introduced in HTML 4.0 to help au...
Preface This article records a problem I encounte...