SoFunction
Updated on 2025-04-07

Example of implementation of react dynamic routing

In React applications, Dynamic Routing usually refers to dynamically displaying or hiding a route (page or component) based on the status of the application or the interaction of the user. This can be achieved in a variety of ways, including using the React Router library, which provides powerful routing management capabilities.

Here are some key points and implementation methods for dynamic routing in React:

1. Use React Router

React Router is a popular library for managing routing in React applications. It allows you to define routing tables and render different components according to changes in URLs.

Install React Router

First, you need to install the React Router library. If you are using Create React App, you can install it via npm or yarn:

npm install react-router-dom 
# oryarn add react-router-dom

Define the route

In your app, you can use<BrowserRouter>(For web applications) or<HashRouter>(For environments that do not support HTML5 history API) to wrap your application and use<Routes>and<Route>to define the route.

//jsx
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom'; 
import HomePage from './HomePage'; 
import AboutPage from './AboutPage'; 
import DynamicComponent from './DynamicComponent'; 


function App() { 
   return ( 
     &lt;Router&gt; 
       &lt;Routes&gt; 
         &lt;Route path="/" element={&lt;HomePage /&gt;} /&gt; 
         &lt;Route path="/about" element={&lt;AboutPage /&gt;} /&gt; 
        {/* Dynamic routing can be implemented here through conditional rendering or other methods */} 
        {/* For example, render a route based on a certain state */} 
          {showDynamicRoute &amp;&amp; ( 
            &lt;Route path="/dynamic" element={&lt;DynamicComponent /&gt;} /&gt; 
          )} 
        &lt;/Routes&gt; 
     &lt;/Router&gt; 
   ); 
}

Note: In the above code example,showDynamicRouteis a hypothetical state variable, you need to set it according to your application logic.

Dynamically add or delete routes

React Router itself does not directly support the ability to dynamically add or delete routes, but you can conditionally render routes by changing the state of your application. This usually involves state management using React (such as useState, useReducer hook, or Redux, etc.).

2. Use status management to control routing

You can use React's state management feature to dynamically display or hide routes based on the status of your application. For example, you can use a boolean value to decide whether to render a route.

//jsx
import { useState } from 'react'; 
import { BrowserRouter as Router, Routes, Route, Link } from 'react-router-dom'; 


function App() { 
const [isLoggedIn, setIsLoggedIn] = useState(false); 


return ( 
   &lt;Router&gt; 
    &lt;nav&gt; 
     &lt;ul&gt; 
      &lt;li&gt; 
        &lt;Link to="/"&gt;Home&lt;/Link&gt; 
      &lt;/li&gt; 
      &lt;li&gt; 
        &lt;Link to="/about"&gt;About&lt;/Link&gt; 
      &lt;/li&gt; 
      &lt;li&gt; 
        &lt;button onClick={() =&gt; setIsLoggedIn(true)}&gt;Login&lt;/button&gt; {/* Assumed login button */} 
      &lt;/li&gt; 
     &lt;/ul&gt; 
    &lt;/nav&gt; 
    &lt;Routes&gt; 
     &lt;Route path="/" element={&lt;HomePage /&gt;} /&gt; 
     &lt;Route path="/about" element={&lt;AboutPage /&gt;} /&gt; 
       {isLoggedIn &amp;&amp; ( 
     &lt;Route path="/dashboard" element={&lt;DashboardPage /&gt;} /&gt; {/*Routes dynamically displayed according to login status */} 
)} 
    &lt;/Routes&gt; 
  &lt;/Router&gt; 
 ); 
}

In this example, when the user clicks the "Login" button,isLoggedInThe state will change totrue,Then/dashboardThe route will be rendered.

3. Use advanced components or hooks

You can also create advanced components (HOCs) or custom hooks to encapsulate routing logic, which can make your code more modular and reusable.

Things to note

  • Make sure your routing definition matches your application logic.
  • Be careful when using conditional rendering to avoid unnecessary re-rendering and performance issues.
  • Consider using React RouteruseNavigateHook for programmatic navigation, rather than relying on links only (<Link>) component.
  • If you need more complex routing logic (such as nested routing, redirection, protected routing, etc.), please refer to ReactRouterr's official documentation (Chinese Documentation)。

This is the end of this article about the implementation example of react dynamic routing. For more related react dynamic routing content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!