This was the way to do it using Pages Router
import { useRouter } from "next/router";
import React from "react";
function ShowroomListings({}) {
const router = useRouter();
const { listingId } = router.query as { listingId: string };
return <div>This is item No. {listingId}</div>;
}
export default ShowroomListings;
Now I imported useRouter from "next/navigation" this time, but ‘.query’ will not work anymore
import { useRouter } from "next/navigation";
import React from "react";
function ShowroomListings({}) {
const router = useRouter();
const { listingId } = router.query as { listingId: string };
return <div>This is item No. {listingId}</div>;
}
export default ShowroomListings;
3
Answers
it worked for me
just passing the right dynamic route value
useRouter
only works on client-component (while your component is a server-component). You can add'use client'
directive to top of the file to useuseRouter
.Update:
query
object has been removed and is replaced byuseSearchParams
Reference:
You cannot use useRouter hook in server component, because it is a client side hook. You need to make you component client inorder to get query parameters.
also nextjs introduce new hook for query parameters which is useSearchParams, you can easily get query param with.