Next.js Discord

Discord Forum

Window not defined in client only component

Answered
aprilia posted this in #help-forum
Open in Discord
const [current, setCurrent] = useState(window.location.href.split("#")[1] ?? navItems[0].id)
Answered by riský
have a look at: https://nextjs-faq.com/browser-api-client-component (but basicly you can't use it directly in useState and should use useEffect or next/dynamic with ssr disabled)
View full answer

5 Replies

Bonaparte's Gull
are you using next/dynamic with ssr: false ?
Australian Freshwater Crocodile
use useEffect with a empty array for dependencies, no?
When accessing client side stuff like window
@aprilia ts const [current, setCurrent] = useState(window.location.href.split("#")[1] ?? navItems[0].id)
have a look at: https://nextjs-faq.com/browser-api-client-component (but basicly you can't use it directly in useState and should use useEffect or next/dynamic with ssr disabled)
Answer
Thanks