React·100 questions

How to safely use window/document in React?

Answer
In SSR, window is unavailable.
Access inside useEffect or check typeof window !== 'undefined'.
Use subscriptions and cleanup for window sizes.
Consider SSR for cookies/localStorage.
In Next 13+, use client components.
Was this answer helpful?

More questions in this topic

Related questions from other topics