Using React Router v6 with Client-Side Routing
Tags: react, womenintech, router, clientsiderouting
Published: 2024-05-28 19:23:01

The differences between Router v5 (version 5) and v6 (version 6) are many. In this blog post, I'll be covering what client-side routing is, and how to use React Router to your benefit when working with this type of routing.

**Client-side routing** is a mechanism through which a user interacts with a webpage. It doesn't require any refreshes or reloads to print new data onto the page; it doesn't require any new GET requests to the server. All of the routing is done with JavaScript. This means: the client's side of the server does ALL of the routing, fetching, and rendering of the information on the DOM for the client to see. The URL should reflect what the user wants to see (ie twitter.com/home, twitter.com/user), which is more intuitive.

Additionally, it's faster overall, which the user always appreciates (not initially, as more must be loaded right off the bat)! 

**React Router** is a client-side router (there are others, such as [Hash Router](https://reactrouter.com/en/main/router-components/hash-router), [TanStack](https://tanstack.com/), or [Hook Router](https://www.npmjs.com/package/hookrouter). React Router allows certain components to be displayed based on which URL is called (ie twitter.com/home will render the Home component, twitter.com/user will render the User component).

**When naming your endpoints,** it is helpful to use the 7 RESTful Routes: 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xexfmp4i57mqeedj0dgj.png)
[Image source](https://medium.com/@shubhangirajagrawal/the-7-restful-routes-a8e84201f206)

Do you want a user to be able to create something new on the page? View other items on the page? Those questions are helpful to ask yourself when naming your URL endpoints.

```
const routes = [
    {
        path: "/",
        element: <App />,
        errorElement: <ErrorPage />,
        children: [
            {
                path: "/home",
                element: <Home />
            },
            {
                path: "/about",
                element: <About />
            },
     //and so on
```
^ Your routes variable should be stored in routes.js, with each component imported at the top of the file. Each route is an object, and 'routes' is an array of these objects. "path: "/home"" means that your URL will read yoururl.com/Home. Makes sense! You can also render more than one element on a specific path as well. 

This example renders App as the parent component, and Home and About as its nested children. App will act as a mechanism through which info is passed down to all its children; anything rendered in App will render in its children as well. The ErrorPage component will render to any of its children that run into an error, letting the user know that something went wrong. 

Import your routes into your index.js file:
```
import React from "react";
import ReactDOM from "react-dom/client";
import { createBrowserRouter, RouterProvider } from "react-router-dom";
import routes from "./routes.js";
import "./index.css"

const router = createBrowserRouter(routes);

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<RouterProvider router={router} />);
```
This links your file with the routes listed to your index.js. 
_createBrowserRouter_ takes the routes array imported from routes.js and creates the router that your website will use for navigation; _RouterProvider_ is the component that takes the prop of 'router' and defines it using the routes in the routes.js file. It will then be available throughout your entire application. 

It took me some time to understand this, especially because there are so many resources for the syntax of v5 versus v6! Take your time - you've got this! xo

_Additional note: To pass props from App to children components, you can use [useOutletContext](https://reactrouter.com/en/main/hooks/use-outlet-context). Here are two additional resources about useOutletContext: [1](https://medium.com/@bobjunior542/react-router-6-advanced-routing-with-useoutlet-and-useoutletcontext-2cfca328b7ac) and [2](https://phantodev.hashnode.dev/using-useoutletcontext-hook-reactjs). This allows you, the developer, to pass props from parent to child while simultaneously using Router (since you aren't explicitly rendering the children components in your parent component anymore, enabling the passing of props)._