![]() |
VOOZH | about |
In web development, the ability to change URLs dynamically without triggering a full page refresh is essential for creating smooth, seamless user experiences. Next.js, a popular React framework, offers built-in features to achieve this efficiently through its client-side routing capabilities. In this article, we'll explore various techniques and approaches to change URLs without page refresh in Next.js applications.
Next.js uses client-side routing to handle navigation between pages without reloading the entire application. It uses the HTML5 History API to manipulate browser history and update the URL in the address bar dynamically.
Next.js provides the <Link> component to enable client-side navigation between pages. You can import the <Link> component from the 'next/link' module and use it to wrap anchor tags (<a>), specifying the href attribute to indicate the target URL.
Step 1: Create a NextJS application using the following command
npx create-next-app my-appStep 2: Navigate to the Project directory
cd my-appStep 3: Install the necessary packages/libraries in your project using the following commands.
npm i bootstrapThe updated dependencies in package.json file will look like:
"dependencies": {
"bootstrap": "^5.3.3",
"next": "14.1.3",
"react": "^18"
}
Example: In example below, we have created a Navbar containing links to different pages, demonstrating how to change the URL without refreshing the page in Next.js.
Step to Run Application: Run the application using the following command from the root directory of the project
npm run devOutput: Your project will be shown in the URL http://localhost:3000/