window.localstorage updated after value managed by useLocalStorage
See original GitHub issueWe’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:
- Created 4 years ago
- Comments:7 (3 by maintainers)
Top GitHub Comments
Confirmed! 🎉
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