React - Step by step react routing.

Certainly! You can achieve dynamic routing in React by combining `react-router-dom` with a layout structure that includes a Header, Side Navbar, Footer, and a dynamic Main Content area. Here's an example of how you can structure your components and handle dynamic routing.



React Routing Image

Here we are using react-router-dom version 6. 

Step 1: Install a library

npm install react-router-dom

Or

yarn add react-router-dom


Step 2. Set up your layout components:
After installation of react-router-dom, route the components in your 'app.js'. Here I have route in 'AdminUIPage.js' instead of 'app.js'. 
First import the Routes and Route from react-router-dom. Here <Routes> tag is used instead of the <Switch> tag from version 6 of react-router-dom.

Setup Layout Component




Step 3: Create your dynamic content components:
Here are two demo components. Those we will route.


Component



Step 4: Create your Sidebar component for dynamic navigation:
Finally, in the Menu, add the Link. And make links above two components.

Link in Menu


Now, when you click on links in the Sidebar, the corresponding components (Sign Up and Forgot Password) will be rendered in the main content area. This approach provides a modular and scalable way to handle dynamic routing in your React application.

Post a Comment

0 Comments