Router slug is first empty then correctly filled #17472
-
Hello there. I'm trying to get the URL slug inside a variable at the page load. As simple as that. But there's a catch. First here is what I have in
And the issue is, on each page load the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This is expected per the docs. Next triggers an update to the router after hydration. You must account for this in your code using something like: const router = useRouter()
const slug = router.query.slug || [];
const target = slug[0];
const [tab, setTab] = useState(target);
useEffect(() => {
if (target) {
setTab(target)
}
}, [target]) |
Beta Was this translation helpful? Give feedback.
This is expected per the docs. Next triggers an update to the router after hydration.
You must account for this in your code using something like: