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');
};
}
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
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");
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With