DEV Community

Cover image for All about routing in React (Part 1) ft. react-router-dom
MILIND D JAIN
MILIND D JAIN

Posted on

All about routing in React (Part 1) ft. react-router-dom

Here, in this post we are going to learn about the routing in react applications using react-router-dom v6.

React-router-dom is a powerful library that is used to handle the routing mechanism in the react application. Its special because in addition to routing it also provides the way to achieve client-side-routing instead of server-side-routing.

Before getting to point on how it provides the client-side-routing, lets discuss in short about client-side-routing and server-side-routing.

So basically in old web apps where hyperlinks (to navigate to other pages) were mentioned using anchor tag, used to make the request to the server and server used to send the page data in response. Even if in react we use anchor tags to navigate we can see that as we click on hyperlinks it will make a request to server and server will return the landing page of our app (where a div with id root is placed), and browser will paint on to that page.
Such kind of routing is called server-side-routing.

Now, another comes client-side-routing, where for getting the content of new page (page to which we want to land), webapp will not make a request to the server.

So how and from where that page content will come from? 🤔
Well in react when we first time hit the domain of our app, all the components are loaded. And these already loaded components will be only served when we navigate to page. Simple !!!

This was about the two types of routing that is used across all web apps.

Now, lets discuss on about "how to start with react-router-dom, to handle routing?"

So, to start with routing we first need to convey our react app all important information about routes and what to show.

This is done by creating routing configuration and wrap our whole app with this routing configuration.

import { Outlet, createBrowserRouter, useLocation } from "react-router-dom";
export const appRouter = new createBrowserRouter([
  {
    path: "/",
    element: <AppLayout />,
    errorElement: <Error />,
  },
  {
    path: "/about",
    element: <About />,
  },
  {
    path: "/contact",
    element: <Contact />,
  },
]);

Enter fullscreen mode Exit fullscreen mode

So, here basically we mentioned -

  1. Number of routes.
  2. Path of routes.
  3. Component to display on each route.

Remember - if component is missed than <Outlet/> will be rendered, which is by default null (means a white screen)

But this is not enough, because still our app is not aware about the paths or routes. To make our app aware about routes and what to show for each path, we need to wrap whole app inside the router provider.

import { RouterProvider } from "react-router-dom";
import AppLayout from "./src/AppLayout";
import ReactDOM from "react-dom/client";
import { appRouter } from "./src/AppLayout";

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  <RouterProvider router={appRouter}>
    <AppLayout />
  </RouterProvider>
);

Enter fullscreen mode Exit fullscreen mode

This AppLayout is the starting point of our react-app.

Now our app is aware about all the routes. 🚀

This was about basic routing, but lets say if requested path is not aware than? What to show? How to handle?
No worries, errorElement will take care of it.

In the next post/article we will resume about nested routes, dynamic routing etc. 😊

Top comments (11)

Collapse
 
ola4987 profile image
Makanjuola

if i change the view of my laptop to mobile and try to make some changes , i am getting this error client.ts:117 [hmr] Failed to reload /src/App.jsx. This could be due to syntax errors or importing non-existent modules. (see errors above)
warnFailedFetch @ client.ts:117
fetchUpdate @ client.ts:470
await in fetchUpdate (async)

(anonymous) @ client.ts:176
handleMessage @ client.ts:174
(anonymous) @ client.ts:91
, but whenever i switch it so full laptop screen , it will load .

Collapse
 
jmilind1234 profile image
MILIND D JAIN

Looks someting related to hot module replacement, can you try restarting of server after you change device from laptop to mobile. @ola4987 . Please let me know if it helps you!

Collapse
 
ola4987 profile image
Makanjuola

thanks for your reply . yes it seems so , but what i do not understand is that it only happens when i resize my screen to mobile, i made a short video recording the challenge that i am facing . but i do not think i can send video to this platform. can i send to your whatsapp or something ?

Thread Thread
 
ola4987 profile image
Makanjuola

i hope you understand my explanation , the issue is not tied to a particular project that i am doing , it is just generally affecting me.

Thread Thread
 
jmilind1234 profile image
MILIND D JAIN

@ola4987 you can send the video to my gmail, jmilind1234@gmail.com.

Thread Thread
 
ola4987 profile image
Makanjuola

thanks sir , i have resolve the issues by downloading an extension to test the self project that i am doing with different phone screen size . thanks sir i really appreciate.

Collapse
 
jmilind1234 profile image
MILIND D JAIN

Please place your valuable suggestions, corrections. 🤝

Collapse
 
ola4987 profile image
Makanjuola

i am having issues with my react , someone should please help me

Collapse
 
jmilind1234 profile image
MILIND D JAIN

Hey @ola4987 , if you don't mind can you share git repo and mention which exact file you were trying to make change into. I would love to help you out If i can.

Kindly drop your git repo details and filename here in comments.

Collapse
 
ola4987 profile image
Makanjuola

the thing is , this is just what i am facing when i am writing code , not that it is tied to some particular project that i am doing .

Collapse
 
ola4987 profile image
Makanjuola

i am currently going crazy and it is no longer funny