I am building a Next.js 13 project with the /app
directory. I have a problem - in the root layout, I have a permanent navbar component in which the component is imported from /components/Navbar.jsx
. Basically inside the Navbar.jsx
, I want to be able to access the slug parameter in url, for ex: localhost:3000/:slug
in which I want the slug id. I have already defined a Next.js 13 page.jsx for that slug. But how do I get the slug id in the navbar component. I also don't want to use window.location.pathname
because it doesn't change when the page routes to a different slug and only does when I refresh.
I have tried the old Next.js 12 method:
//components/navbar.jsx;
import { useRouter } from "next/navigation";
export default function Navbar () {
const router = useRouter();
const { slug } = router.query;
useEffect(() => {
console.log(slug);
}, []);
return <p>Slug: {slug}</p>
}
However it does not work.