Next.js Discord

Discord Forum

how can i use localStorage inside my client component? i get this error all the time

Unanswered
American Robin posted this in #help-forum
Open in Discord
American RobinOP
- error ReferenceError: localStorage is not defined

27 Replies

American RobinOP
im storing data like: language in the localStorage
the component get pre rendered on the server right? if im not wrong, how do i have to work with this
Put it into useEffect
American RobinOP
oke ill try that
Your client component is pre-rendered on the server, but useEffect is only executed on the client side after hydration
American RobinOP
i dont get the error anymore, but its not workin properly atm
mhmm
atleast the error is gone
A flash is caused because the useEffect wonÒ€ℒt be executed instantly. It happens after hydration, you can add a skeleton to prevent the flash
American RobinOP
i would make a state, loading right?
to check if is loading true or not
and put the skell then there
Yeah, just determine yourself :RyoXD:
American RobinOP
oh it worked now
but as u said
there is still thhe "flash"
where it changes
after 1s
American RobinOP
@fuma πŸ’™ joulev as i can see my custom hook is a problem
i cant use a custom hook with the local storage
What do you mean by custom hook?
@American Robin there is still thhe "flash"
ThatÒ€ℒs why you will need a skeleton component, so that the flash can look better for users
@fuma πŸ’™ joulev What do you mean by custom hook?
American RobinOP
useLocalStorage hook
@fuma πŸ’™ joulev ThatÒ€ℒs why you will need a skeleton component, so that the flash can look better for users
American RobinOP
its only like rly 0.2ms ? smth like that rly instant, the skelton is showing only for that small time
is that ok tho?
Of course, itÒ€ℒs fine