[Fixed] Issue with navigating to a new page in Next.js – Next.js

by
Ali Hasan
android-jetpack-navigation next.js reactjs

Quick Fix: To fix this issue, you need to create a swap folder inside app and add a page.js file inside it. This will create a /swap route, and you can place the content of your swap.js file into the page.js file.

The Problem:

When using router.push to navigate to a different page in Next.js, the desired navigation does not occur. The button component is triggered to push to a page named example.js, but no navigation happens. Despite importing the necessary modules and defining the router variable, the router.push function does not navigate to the intended page.

The Solutions:

Solution 1: Using the Correct Folder Structure

Next.js has updated its routing system in v13. In the new system, routes are defined by folders and a page.js file within them, rather than by individual files under the pages directory.

To fix your issue, you should create a folder named swap under the app directory. Within the swap folder, create a file named page.js. Move the contents of your current swap.js file into page.js within the swap folder.

This new file structure aligns with the latest Next.js routing system, allowing your router.push call to navigate to the swap page correctly.

Q&A

What is the issue with router.push function?

Router.push is not working as the current file structure does not match the expected Next.js routing setup using the app router

How to fix this issue?

Create a folder called swap under app and move the content of swap.js to a new page.js file inside the swap folder.

Video Explanation:

The following video, titled "What Next.js doesn't tell you about caching... - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

In this video we discuss some issues and confusion around how page ... navigation. Github Issue about stale pages: https://github.com/vercel/next ...