Using import { useRouter } from "next/router";
as import { useRouter } from "next/navigation";
throws "Argument of type '{ pathname: string; query: { search: string; }; }' is not assignable to parameter of type 'string'."
const router = useRouter();
const [searchInput, setSearchInput] = useState("");
const search = (e) => {
router.push({
pathname: '/search',
query: {
search: searchInput,
},
})
}
NextJS documentation
From docs:
A component used useRouter outside a Next.js application, or was rendered outside a Next.js application. This can happen when doing unit testing on components that use the useRouter hook as they are not configured with Next.js' contexts.
Migrating from the pages directory:
usePathname()
useSearchParams()
router.events is not currently supported.Here is the solution: https://beta.nextjs.org/docs/api-reference/use-router