React Router V6 Updates

React Router V6 Updates

React Router V6 Changes

I 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 Easier

3.1 The specific changes are as follows:

  • <Route children> has been changed to accept child routes.
  • Simpler matching rules than < Route exact > and < Route strict >.
  • < Route path > The path hierarchy is clearer.
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-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 /> }
      ]
    },
    // Redirect { path: 'home', redirectTo: '/' },
    // 404 Not Found { path: '*', element: <NotFound /> }
  ]);
  return element;
}

Summarize

This 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:
  • Detailed explanation of routing and nested routing in react-router-domV6

<<:  How to disable web page styles using Firefox's web developer

>>:  Details about the like operator in MySQL

Recommend

JavaScript two pictures to understand the prototype chain

Table of contents 1. Prototype Relationship 2. Pr...

Vue3 navigation bar component encapsulation implementation method

Encapsulate a navigation bar component in Vue3, a...

JavaScript imitates Xiaomi carousel effect

This article is a self-written imitation of the X...

How to use HTML 5 drag and drop API in Vue

The Drag and Drop API adds draggable elements to ...

Mysql 8.0 installation and password reset issues

Mysql 8.0 installation problems and password rese...

Explanation of the process of docker packaging node project

As a backend programmer, sometimes I have to tink...

Win10 configuration tomcat environment variables tutorial diagram

Before configuration, we need to do the following...

How to install Django in a virtual environment under Ubuntu

Perform the following operations in the Ubuntu co...

How to view and configure password expiration on Linux

With the right settings, you can force Linux user...

Summary of the use of element's form elements

There are many form elements. Here is a brief sum...

Attributes and usage of ins and del tags

ins and del were introduced in HTML 4.0 to help au...

JavaScript gets the scroll bar position and slides the page to the anchor point

Preface This article records a problem I encounte...