Next.js Discord

Discord Forum

Accessing local storage in a client component gives error if used outside useEffect

Unanswered
Burmese posted this in #help-forum
Open in Discord
BurmeseOP
I have a user token stored in localStorage. I read this token from local storage and if the token is successfully read and verified, the page displays a logout button, otherwise it displays sign up and log in buttons.

If I read the token with a useEffect, then for a second after the page renders, it displays log in buttons because we are getting the token from localStorage after the page has rendered. I want to get the local storage token first and then display the page.

I simply turned the component into a client component and it works fine. The token is being successfully read and console logged. But I still get these errors displayed.

1 Reply

Asian black bear