Next.js Discord

Discord Forum

ReferenceError: localStorage is not defined in Next.js during server-side rendering

Unanswered
Cuvier’s Dwarf Caiman posted this in #help-forum
Open in Discord
Cuvier’s Dwarf CaimanOP
I'm encountering an issue with my Next.js application where I'm getting a ReferenceError: localStorage is not defined error. This occurs specifically during server-side rendering (SSR). I've wrapped my code with checks like if (typeof window !== 'undefined') to ensure client-side code is executed only on the client, but the error persists

4 Replies

Cape horse mackerel
did you try to use useEffect and access localStorage there?
Giant panda
@Cape horse mackerel did you try to use `useEffect` and access `localStorage` there?
Cuvier’s Dwarf CaimanOP
Yes i have used but after that my redux state was not getting updated
@Giant panda https://nextjs-faq.com/browser-api-client-component
Cuvier’s Dwarf CaimanOP
Maybe i should start reading more docs i have started project in a rush and learned nextjs on the go . BTW thanks for the doc