Next.js Migrating from React Router
Last Updated :
24 Apr, 2025
Are you using React Router in your web application and planning to switch to Next.js? Don't worry, the migration process is easy and we've got you covered! This blog post will guide you through the process and help you understand the benefits of doing so.
What is Next.js?
Next.js is a React-based framework used for building server-side rendered (SSR) applications. It provides a lot of features out of the box, such as automatic code splitting, server-side rendering, and static site generation. Next.js also comes with built-in support for TypeScript, CSS modules, and API routes.
Why migrate from React Router to Next.js?
While React Router is a great library for client-side routing, it doesn't provide server-side rendering out of the box. This means that when a user visits a page, the browser has to wait for the JavaScript to load before rendering the page. This can lead to slower page load times and poor user experience.
On the other hand, Next.js provides server-side rendering by default, which can improve page load times and provide a better user experience. Additionally, Next.js provides automatic code splitting, which means that only the necessary JavaScript code is downloaded when a user visits a page, further improving performance.
Create NextJS Application:
Install Next.js:
npx create-next-app@latest
After the installation is complete start the development server:
npm run dev
Open to http://localhost:3000 to view your application
Migrating to Next.js Routing: We will do it in 2 steps:
Step 1: Creating pages: The first step in migrating your app from React Router to NextJS is to convert your existing code. The good news is Next.js uses the file system to define routes. When you create a file in the pages directory, Next.js automatically creates a route based on the file name. For example, if you create a file named about.js in the pages directory, Next.js will automatically create a route for /about.
To migrate your React Router app to Next.js, you need to create a page for each route in your app. For example, if you have a route in your React Router app defined like this:
Syntax:
<Route path="/about" component={About} />
For example, you may have the following directory or project structure:
Create a file named about.js & contact.js in the pages directory and define the About & Contact component in that file:
about.js
JavaScript
const About = () => {
return (
<div>
<h1>About</h1>
<p>This is the about page.</p>
</div>
);
};
export default About;
contact.js
JavaScript
function Contact() {
return (
<div>
<h1>Contact</h1>
<p>This is the contact page.</p>
</div>
);
};
export default Contact;
NextJS will automatically handle routing based on the file names in your pages directory.
File | Routes |
---|
about.js | /about |
contact.js | /contact |
index.js | / |
In each file, you'll need to export a React component that represents your page.
Step 2: Adding links: In a React Router app, you would use the Link component to create links to other pages in your app. In Next.js, you can use the Link component as well, but instead of using the to prop, you use the href prop. For example, if you have a link in your React Router app defined like this:
Syntax:
<Link to="/about">About</Link>
You would define the link in your Next.js app like this:
index.js
JavaScript
import Link from "next/link";
const Home = () => {
return (
<div>
<h1>Welcome to my home page</h1>
<ul>
<li><Link href="/about">About</Link></li>
<li><Link href="/contact">Contact</Link></li>
</ul>
</div>
);
};
export default Home;
Output:
Dynamic Routing: NextJS provides a range of routing features that make it easier to handle complex routing scenarios. In a React Router app, you can define dynamic routes using a colon followed by a parameter name.
Syntax:
<Route path="/users/:userId" component={User} />
In Next.js, you can define dynamic routes in a similar way, but using square brackets instead of a colon. For example in the pages directory create a new directory user, inside it creates a new file [userId].js
pages/users/[userId].js
JavaScript
// pages/users/[userId].js
import { useRouter } from "next/router";
const User = () => {
const router = useRouter();
const { userId } = router.query;
return (
<div>
<h1>User {userId}</h1>
</div>
);
};
export default User;
Note that in the above code, we use the useRouter hook from Next.js to access the userId parameter from the query string.
The route /users/123 will have the following query object:
{ "userId": "123" }
To test the dynamic routes you may make the following changes in the index.js file
JavaScript
import Link from "next/link";
const Home = () => {
return (
<div>
<h1>Welcome to my home page</h1>
<ul>
<li><Link href="/about">About</Link></li>
<li><Link href="/contact">Contact</Link></li>
</ul>
{/*Test Dynamic routes */}
<br />
<ul>
<li><Link href="/users/1">User 1</Link></li>
<li><Link href="/users/2">User 2</Link></li>
<li><Link href="/users/3">User 3</Link></li>
</ul>
</div>
);
};
export default Home;
Output:
In conclusion, migrating an app from React Router to Next.js can simplify the routing and navigation process and improve the performance of the application. To learn more check out this official Next.js documentation. I hope this article helped you migrate your app from React Router to Next.js.
Similar Reads
Next JS Upgrading: Migrating from Vite
Migrating from Vite to Next. By utilizing Next, your React app can benefit from an increase in performance. js has some power full features like server-side rendering(SSR), static site generation (SSG) and built-in API routes. There are some steps in this migration to move your project structure and
8 min read
Next.js Migrating from Gatsby
If you use Gatsby and want to switch to the newest NextJs ecosystem, you've come to the right spot. Here, you can learn all about the differences between the two technologies and discover why it's the best moment to switch from Gatsby to NextJs. NextJS Migrating from GatsbyGatsby is an open-source f
6 min read
How to Migrate from Create React App to Next JS ?
Migrating from Create React App to Next.js is a structured process that includes setting up a Next.js project, reorganizing your files, updating dependencies, and utilizing Next.js features for better performance and server-side rendering.Prerequisites:NPM & Node.jsReact JSNext JSApproach To mig
2 min read
Migrate to React Router v6
React Router v6 brings significant changes and improvements over its predecessor, offering a more intuitive API and better performance. Migrating to React Router v6 might seem daunting, but with proper guidance, the transition can be smooth and beneficial for your React applications. Enhancements in
3 min read
Navigate Component in React Router
In React applications, navigation between different pages or views is an essential part of creating dynamic user interfaces. React Router is a popular library used for handling routing and navigation. One of the key features of React Router is the Navigate component, which allows for programmatic re
7 min read
NPM React Router Dom
React Router DOM is a powerful routing library for React applications that enables navigation and URL routing. In this article, we'll explore React Router DOM in-depth, covering its installation, basic usage, advanced features, and best practices. What is React Router DOM?React Router DOM is a colle
2 min read
react-router-dom - NPM
react-router-dom is an important library for handling routing in React applications. It allows you to navigate between different components and manage the browser history. Here, we cover everything you need to know about react-router-dom, from installation using npm to implementing routes in a React
4 min read
Link Component in React Router
React Router is a powerful library in ReactJS that is used to create SPA (single-page applications) seamlessly. One of the components of the React Router is Link. In this article, we will be seeing the working of the Link component, various props, and usage of the Link component within React Router
5 min read
Migrating from Create React App to NextJS: A Practical Guide
Migrating from Create React App (CRA) to Next.js involves transitioning from a client-side rendered React application to a framework that offers server-side rendering, static site generation, and built-in routing capabilities. This guide provides a step-by-step approach to help you effectively migra
4 min read
HashRouter in React Router
React Router is a library in React JS (Frontend Framework). It is commonly used for handling the navigation and routing in a web application. In this post, we will learn about the "HashRouter" component provided by the React Router library. Table of Content What is HashRouter?Features of HashRouterD
2 min read