How to _force_ `useClient` to avoid "ReferenceError: localStorage is not defined"?
Answered
Gazami crab posted this in #help-forum
Gazami crabOP
The component has
But since this was anyway intended, yet
NextJS users shouldn't just "have to live with" such "errors", right?
use client
at top of the file and at the top of its React compo function. Still, accessing localStorage
during a useState
call trigger NextJS-side error like screenshotted. Of course, the page still works fine as the component rendering is then left to the browser. But since this was anyway intended, yet
use client
didn't force it, what will? NextJS users shouldn't just "have to live with" such "errors", right?
Answered by Asian black bear
Components marked with
'use client'
will still be prerendered on the server which is why you see the error happen. Either you move the logic into an effect or you load the component dynamically with dynamic
and ssr: false
as an actual client-side component that will never be prerendered.1 Reply
Asian black bear
Components marked with
'use client'
will still be prerendered on the server which is why you see the error happen. Either you move the logic into an effect or you load the component dynamically with dynamic
and ssr: false
as an actual client-side component that will never be prerendered.Answer