18

I am trying to redirect the user to the search results, but router.push() is not working apparently. I changed url to page that don't exist and the route changes, but when it comes to using the url of page already created, nothing happens.

I tried doing something like this, but it's not working either.

import { useRouter } from 'next/router';
const router = useRouter();

const search = () => {
    router.push('/search');
    };
}
9
  • could you show the error ? Commented Apr 21, 2022 at 22:34
  • How do you call search() function ? Commented Apr 21, 2022 at 22:38
  • @Fr0z3n7 <button onClick={search} className={"btn"}>Search</button> Commented Apr 21, 2022 at 23:04
  • Hi, please check the answer stackoverflow.com/a/57474576/7369564 . i hope this helps. Commented Apr 22, 2022 at 4:30
  • 1
    @Saurabh yes it is. I explained further my problem here link Commented Apr 25, 2022 at 11:19

5 Answers 5

23

If you are using Nextjs13, please be aware to import useRouter from next/navigation:

❌ import { useRouter } from 'next/router';

to:

✅ import { useRouter } from 'next/navigation';

Documentation link

Sign up to request clarification or add additional context in comments.

4 Comments

Do you have a link to documentation that points this out?
Good point. Added link.
This only applies if you're migrating from the pages directories to app directories - if you're still using pages, this doesn't apply.
doesn't make any difference, still doesn't work.
17

The solution here is to prevent default function by doing something like this:

import { useRouter } from 'next/router';
const router = useRouter();

const Search = (e) => {
        e.preventDefault()
        router.push("/search");
    };

4 Comments

doesn't fix it for me, worked the first time, doesn't work on subsequent times...
Thank you!! I knew it would be my own stupidity. Glad someone else made the same mistake :)
The preventDefault() was the trick for me with a client component as well, using next/navigation
In my case, I forgot that the button to trigger Router.push was inside a form, and so clicking the button triggered the form submission rather than routing to the page I wanted it to go to. The preventDefault() solved that for me
2

From next js 13 onwards use router should be imported from next/navigation

✅ import { useRouter } from 'next/navigation';

calling router.push for updating the route in shallow(eg: updating query params on the client) might cause the page to reload. Use native APIs (window.history) for handling them.

  window.history.pushState(null, '', `?${...YOUR_QUERY_PARAMS}`)

or

 window.history.replaceState(null, '',NEW_PATH)

refer https://nextjs.org/docs/app/building-your-application/routing/linking-and-navigating

1 Comment

Not exactly. In Next.js 13 and later, the new useRouter from next/navigation is intended for app directory (app router) components, which are typically server components or client components under the /app directory. If you are using the pages directory (as in your code: login.tsx), you should continue to use: import { useRouter } from 'next/router'; If you are using the app directory (with React Server Components and Client Components), then use: import { useRouter } from 'next/navigation';
1

Just replace it with window.location.href = "/"

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
This is the correct answer, because nextjs router is buggy
0

Adding an prevent default instruction on the event param will solve the problem since the button is considered as of submit type by default.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.