React Router version 6

Download & installation


npm install react-router-dom@6

Connect with the URL

import { BrowserRouter } from "react-router-dom";

<BrowserRouter>
<App/>
</BrowserRouter>
 

Add links

import { Link } from "react-router-dom";
<Link to="/register">Register</Link> 
 

Add routes

 
import {
BrowserRouter,
Routes,
Route
} from "react-router-dom";


<BrowserRouter>
  <Routes>
    <Route path="/" element={<App />} />
    <Route path="register" element={<Register />} />
    <Route path="login" element={<Login />} />
  </Routes>
</BrowserRouter>

URL params

import { useParams } from "react-router-dom";
let params = useParams();
console.log(params.id)
 

Search params

import {  
useSearchParams
} from "react-router-dom";

let [searchParams, setSearchParams] = useSearchParams();
 

Navigating programmatically

import { useNavigate } from "react-router-dom";
let navigate = useNavigate();
navigate("/login");
© 2016 - 2022, All Rights are Reserved.