React Router 6 πŸš€

Udemy John React Router 6

Docs

React Router Docs

To install react-router-6

npm install react-router-dom@6

// For latest version
npm install react-router-dom

First page

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

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<div>home page</div>} />
        <Route
          path="testing"
          element={
            <div>
              <h2>testing </h2>
            </div>
          }
        />
      </Routes>
    </BrowserRouter>
  )
}

export default App

Components

If we want to navigate through our app internally, then we use Link. Link is similar to <a href but is used to open different page in our app and not external URLs like google.com

Let's say I am in Home page, / and I want to navigate to About page, /about then we can either type the /about in URL or use Link (when clicked will take us to /about page).

If we need to go to external URLs like google.com, then we still need to use <a href

Error Page

If we navigate to non-existing URL within our app then it shows a blank page by default which is not a good user experience. We can define a wildcard route (*) which goes to the page we say if none of the above routes match

We want a navbar/ footer that will be common to all the pages (Home, about, and products). We can achieve it by

  • nesting these routes inside common path. For example, we want all routes to start at /

    • /

    • /about

    • /products

  • Once we nest them, we also need to use outlet section inside main route / so that this <Outlet/> will be either Home, Or About or Products

App.js
Home.js (Main Page that contains Outlet)
Navbar (inside Home)

Understand this diagrammatically here Explained Shared layout diagrammatically

We need to know which link is selected, so we often add an active class. We can make use of NavLink provided by react-router-dom instead of Link. The difference is, the NavLink will show us if selected link is active or not.

So our Navbar can look like this, let's name it StyledNavbar

We can use inline style or className as above.

URL Params (useParams)

Let's say we have products page that contains a list of products, and when we click on a product it will take us to details of individual product. We can make use of useParam() hook to see dynamically what ID is passed

Let's say we have a /login route that displays a login form, and when user logs in by filling creds, it should navigate us to Dashboard page. In Dashboard page, if user entered the creds, it should show "Hello, name", else it should display "Hello"

Protected Route

Let's say we don't want to show the Dashboard page if user is not logged in, then we can protect that route like this

We can use <Navigate to=""/> to navigate to any page.

Two ways we can navigate to different pages

  • useNavigate() can be used outside jsx

  • <Naviage/> can be used inside jsx

Shared Layout for Products

We were using this structure for products and :productIdNavigate (useNavigate)

Both of these routes "products" and "products/:productId" can be turned into nested route so that we know it is a group.

We do this similar to what we did in SharedLayout above.

The parent SharedProductLayout contains only the placeholder for underlying child components and that placeholder is <Outlet/>

Explained Shared layout diagrammatically

diagrammatically explained shared layout

Last updated

Was this helpful?