question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

window.localstorage updated after value managed by useLocalStorage

See original GitHub issue

We’d been using this hook for a while, but it broke our setup after #16 was merged (we updated a couple of months after the merge).

Specifically, we perform user authentication, and save the authToken in localStorage using this hook. Now setting the authToken triggers a rerender in our tree, which sees that the value managed by useLocalStorage has been set, which triggers a request to be sent out using the apollo client.

Our authentication setup is configured with an apollo link which sets the Authorization header based on the authToken in localStorage. This is where our setup breaks down. While the token is set in our React state tree (which causes the request to be fired in the first place), the token hasn’t actually been persisted to localStorage yet.

Due to the way useEffect works, this hook only persists to localStorage in the next render cycle.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
edorivaicommented, Oct 28, 2019

Confirmed! 🎉

0reactions
vidjuheffexcommented, Jun 26, 2021

I’m still seeing this behavior… I reverted to 3.3.2 to solve this for myself… is this expected? I can’t use the latest versions because initial value isn’t set into local storage

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using localStorage with React Hooks - LogRocket Blog
In this guide, we cover how to use localStorage to persist a user's form ... and run the following command to create a...
Read more >
Window.localStorage - Web APIs | MDN
Value. A Storage object which can be used to access the current origin's local storage space. Exceptions.
Read more >
React, update and detect localStorage changes with a custom ...
You should set up an event listener for storage changes in order to have the updated value. See the code below and the...
Read more >
How to Save State to LocalStorage & Persist on Refresh with ...
We can use this so that any time the state value changes, we'll store that updated value in localStorage. Inside our useEffect hook,...
Read more >
How to update localStorage() with vanilla JavaScript
You can replace the existing value by using setItem() again with the new value. // Update the value to "tuna" localStorage.setItem(' ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found