Home > frontend > How to Implement Dynamic Routing in ReactJS with React Router.
How to Implement Dynamic Routing in ReactJS with React Router.
Dynamic routing is a concept that redirects a website visitor from a page to another based on the value passed on the URL.
By :Thomas Inyang🕒 11 Jan 2025

Introduction
Routing in web development is an important concept of a website, it gives your website a map to direct visitors to the appropriate location based on their actions or inputs. As front-end developers, it’s very important to utilize this concept as it’s beneficial to the users who intend to share a post or product-item URL with anyone.
In this post, you will learn how to route dynamically based on specific input, such as a blog slug, using React Router. This powerful tool simplifies dynamic routing in ReactJS applications, making it straightforward, efficient, and highly effective.
Prerequisite:
- Knowledge of ReactJs.
- React App with Vite.
What is Dynamic Routing?
Dynamic routing is the ability of a website to display different page components based on the URL path which gives room for more flexibility. An example of dynamic routing is a blog website, where you can have a single route that displays different posts based on their unique slugs or IDs.
How to Implement Dynamic Route In a React App using React Router.
In the section, you will need to do the following:
Step1
Install React App.
This will install the React App when you follow the prompts. You can either choose JavaScript or Typescript as the programming language tool. For this tutorial choose Typescript.
Step2:
Install React Router.
This will install the react-router which will enable us to navigate from one page to another.
Step3:
Update the main.tsx file with this.
In this code, you’ve been able to set up ReactRouter, and you can navigate between pages by specifying the path “/path” and element component. In this case, this is the homepage path "/”.
Step4:
Create a type file in the src dir, types.d.ts, and enter the following code block
This will be used to strictly type the blog post data.
Step5:
Create mock data for the blog.
src/mockData.ts
The Post interface is imported and used as a “type” for the mockData.
Step6:
Create a components folder and a file AllPosts.txs in it, and enter this code block
This is the entry file that shows all the blog posts (mock) and when the post slug is clicked, the user will be redirected (dynamic routing) to the post details page.
Step7
Create a post folder with a PostDetails.tsx file and paste this code
src/post/PostDetails
This component uses React Router's `useParams` hook hat extracts dynamic parameters from the current URL. In our case, it pulls out the `slug` portion, which we then use to find the matching post from our data.
Step 8: Updating the App Component
Let's update our main App component to serve as the container for our blog listing. Modify `src/App.tsx`:
Step9
Update the main.tsx file.
The magic happens in this line: `<Route path="/post/:slug" element={<PostDetails />} />`. The colon syntax (`:slug`) designates a dynamic parameter in the URL. When a user navigates to a URL like `/post/understanding-react-hooks`, React Router captures `understanding-react-hooks` as the `slug` parameter, which becomes accessible in the `PostDetails` component via the `useParams` hook.
Step10
Test run the app from the terminal npm run dev. After loading is complete, when you click on a post slug you will be redirected to the page of the params (slug).
Best Practices for Dynamic Routing In ReactJS.
To ensure your dynamic routing implementation is robust, maintainable, and user-friendly:
1. Use Descriptive, SEO-Friendly URL Parameters
Instead of using opaque IDs:
- ❌ `/post/12345`
- ✅ `/post/understanding-react-hooks`
2. Implement Proper Loading States
Users should receive feedback while data for the dynamic route is loading:
3. Handle Invalid Parameters Gracefully
Always account for the possibility that the URL parameter doesn't match any existing content:
4. Implement Redirects for Changed URLs
If your content URLs change (e.g., due to title updates), implement redirects to preserve SEO and user bookmarks:
Dynamic routing is very useful for a website. It enhances the user experience by allowing seamless transitions between one to another and making your application feel faster and more responsive.
Conclusion
Throughout this comprehensive guide, we've explored how dynamic routing in React transforms static web pages into interactive, deeply linked applications. By implementing React Router with URL parameters, you can create flexible, scalable solutions for a wide range of use cases—from blogs and e-commerce sites to dashboards and documentation platforms.
The techniques you've learned enable:
- Enhanced user experiences through direct URL access to specific content
- Improved shareability via clean, descriptive URLs
- Better SEO performance with permanent, crawlable links
- Reduced code redundancy by rendering different content with the same component
- Seamless state preservation through bookmarkable, deep-linkable pages
As you continue developing React applications, remember that effective routing is not merely a technical implementation detail—it's a fundamental aspect of user experience design. The thoughtful URL structures you create become the pathways through which users navigate, share, and return to your application.
Share with others