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.

Persist in localStorage, but do not upgrade React state

See original GitHub issue

Hi @astoilkov thanks for this great library.

I ran into an edge case and was wondering if that’s a feature you may wanna support in the future:

In my case, I want to call setValue for storing the value in local storage, but without updating React’s state. In other words, when calling setValue, I do not want a state update and thus no re-render. Scenario: I only want to use useLocalStorageState to write to the local storage and to pick up the value from the storage for the initial rendering, in between I do not care about the value anymore.

This could probably be solved with another property in the options object, e.g. { onlyWrite: true } which would prevent the internal setState call. However, I can also understand if that’s something the library is no supposed to offer, because it’s after all a solution for syncing the local storage to React’s state.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
astoilkovcommented, Mar 23, 2022

I understand your scenario. Give me some time to think about this. I’ve seen people wanting to get the state only initially and then don’t update it but I should think if there is a unifying solution that can fit all scenarios.

0reactions
astoilkovcommented, May 28, 2022

I’m closing this because of a lack of activity.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Persisting React State in localStorage - Josh W Comeau
A common thing in React development is that we want to store a bit of React state in localStorage, and re-initialize from that...
Read more >
How to Save State to LocalStorage & Persist on Refresh with ...
Step 3: Getting a stored value from localStorage and loading it into React state. Finally to make sure our state is persisted any...
Read more >
Local storage not updating React - Stack Overflow
You are trying to save allusers to the localStorage right after setAllUsers() but setState is asynchronous. The value does not have to be ......
Read more >
How to Persist React State in Local Storage - Felix Gerschau
Step 1: Initialize the state from local storage ... In the example, I'll use React hooks. The first step is to initialize the...
Read more >
Save State to LocalStorage & Persist on Refresh with React.js
Learn how to save React state and load it when a page refreshes using localStorage. We'll learn how to set up a simple...
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