how can i use localStorage inside my client component? i get this error all the time
Unanswered
American Robin posted this in #help-forum
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 hydrationAmerican 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 flashAmerican 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
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?
ThatΓ’β¬β’s why you will need a skeleton component, so that the flash can look better for users
American RobinOP
useLocalStorage hook
ye true
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