Next.js Migrating from React Router
Last Updated :
28 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
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
React Fundamentals
React IntroductionReactJS is a component-based JavaScript library used to build dynamic and interactive user interfaces. It simplifies the creation of single-page applications (SPAs) with a focus on performance and maintainability. "Hello, World!" Program in ReactJavaScriptimport React from 'react'; function App() {
6 min read
React Environment SetupTo run any React application, we need to first setup a ReactJS Development Environment. In this article, we will show you a step-by-step guide to installing and configuring a working React development environment.Pre-requisite:We must have Nodejs installed on our PC. So, the very first step will be
3 min read
React JS ReactDOMReactDOM is a core React package that provides DOM-specific methods to interact with and manipulate the Document Object Model (DOM), enabling efficient rendering and management of web page elements. ReactDOM is used for: Rendering Components: Displays React components in the DOM.DOM Manipulation: Al
2 min read
React JSXJSX stands for JavaScript XML, and it is a special syntax used in React to simplify building user interfaces. JSX allows you to write HTML-like code directly inside JavaScript, enabling you to create UI components more efficiently. Although JSX looks like regular HTML, itâs actually a syntax extensi
5 min read
ReactJS Rendering ElementsIn this article we will learn about rendering elements in ReactJS, updating the rendered elements and will also discuss about how efficiently the elements are rendered.What are React Elements?React elements are the smallest building blocks of a React application. They are different from DOM elements
3 min read
React ListsIn lists, React makes it easier to render multiple elements dynamically from arrays or objects, ensuring efficient and reusable code. Since nearly 85% of React projects involve displaying data collectionsâlike user profiles, product catalogs, or tasksâunderstanding how to work with lists.To render a
4 min read
React FormsIn React, forms are used to take input from users, like text, numbers, or selections. They work just like HTML forms but are often controlled by React state so you can easily track and update the input values.Example:JavaScriptimport React, { useState } from 'react'; function MyForm() { const [name,
4 min read
ReactJS KeysA key serves as a unique identifier in React, helping to track which items in a list have changed, been updated, or removed. It is particularly useful when dynamically creating components or when users modify the list. When rendering a list, you need to assign a unique key prop to each element in th
4 min read
Components in React
React ComponentsIn React, components are reusable, independent code blocks (A function or a class) that define the structure and behavior of the UI. They accept inputs (props or properties) and return elements that describe what should appear on the screen.Key Concepts of React Components:Each component handles its
4 min read
ReactJS Functional ComponentsIn ReactJS, functional components are a core part of building user interfaces. They are simple, lightweight, and powerful tools for rendering UI and handling logic. Functional components can accept props as input and return JSX that describes what the component should render.Example:JavaScriptimport
4 min read
React Class ComponentsClass components are ES6 classes that extend React.Component. They allow state management and lifecycle methods for complex UI logic.Used for stateful components before Hooks.Support lifecycle methods for mounting, updating, and unmounting.The render() method in React class components returns JSX el
3 min read
ReactJS Pure ComponentsReactJS Pure Components are similar to regular class components but with a key optimization. They skip re-renders when the props and state remain the same. While class components are still supported in React, it's generally recommended to use functional components with hooks in new code for better p
4 min read
ReactJS Container and Presentational Pattern in ComponentsIn this article we will categorise the react components in two types depending on the pattern in which they are written in application and will learn briefly about these two categories. We will also discuss about alternatives to this pattern. Presentational and Container ComponentsThe type of compon
2 min read
ReactJS PropTypesIn ReactJS PropTypes are the property that is mainly shared between the parent components to the child components. It is used to solve the type validation problem.Type Safety: When the wrong data type is passed in the component, prototypes help find the issues.Better Debugging: During development, t
5 min read
React Lifecycle In React, the lifecycle refers to the various stages a component goes through. These stages allow developers to run specific code at key moments, such as when the component is created, updated, or removed. By understanding the React lifecycle, you can better manage resources, side effects, and perfo
7 min read
React Hooks
Routing in React
Advanced React Concepts
React Projects