state is not persisted to LS during initial render with defaultValue
See original GitHub issueconst [locale, setLocale] = useLocalStorageState<'en-US' | 'es'>(
'locale', 'en-US',
)
The above will not actually persist the default value to the local storage key until setLocale
is called. I would expect the library to initialize the LS key with the default value if it doesn’t already exist.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
React input defaultValue doesn't update with state
Another way of fixing this is by changing the key of the input. <input ref="text" key={this.state.awayMessage ? 'notLoadedYet' : 'loaded'} onChange={this.
Read more >@State's initial value doesn't cha… | Apple Developer Forums
I'm having trouble understanding how to reset a @State property to its initial value. In my app, I have one view that sets...
Read more >My useState always renders the default value when ... - Reddit
Hey. I am creating a page where when the value of the useState variable is set to true, the component loads, but when...
Read more >Form not populating with defaultState after re-render #2282
The values are stored in a state atom called userInputState, which is passed down to a child form (using react-hook-form) as "initialState." The ......
Read more >Dockerfile reference - Docker Documentation
Dockerfiles use a simple DSL which allows you to automate the steps you would normally manually take to create an image.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That makes sense. I will change the behavior. Thanks.
Well I’d expect it to work like
React.setState
where once you set the value, it’s persisted. Very confusing behavior for you to supply a default value and not have it automatically persist to LS. Like imagine you derive a user’s locale based on what URL they land on etc and then want to persist that, feeding it in as the default value. Without this behavior the only way to get it to persist is to switch it to something else and then back to the value you actually want.