Async storage lookup/default value issue
See original GitHub issue@astoilkov: A React app can have a query parameter (e.g. ?appId=123
) which is persisted using this use-local-storage-state
hook.
A default value is also set (-1
). When the state is -1
, an error is thrown (no App ID is set, the app needs an App ID to function).
The problem is that the use-local-storage-state
is asynchronous: In the first render the value is still the default value (-1
), in the 2nd render the value is the one retrieved from local storage.
This means that in the first render the app already shows an error message, as the error will also stop further renders.
How can I get around this?
Issue Analytics
- State:
- Created 2 years ago
- Comments:20 (10 by maintainers)
Top Results From Across the Web
How to set default value and if not then the ... - Stack Overflow
I am using the async storage to get the value of recent. If the value is not found in async storage then I...
Read more >API | Async Storage - GitHub Pages
Sets a string value for given key . This operation can either modify an existing entry, if it did exist for given key...
Read more >AsyncStorage - React Native
On iOS, AsyncStorage is backed by native code that stores small values in a serialized dictionary and larger values in separate files. On ......
Read more >util.AsyncStorage. Developer's guide - Yandex
Search on a map ... AsyncStorage. Extends util.Storage. Storage that provides asynchronous access to key values. ... Parameter, Default value, Description ...
Read more >A guide to React Native's AsyncStorage - LogRocket Blog
AsyncStorage is an unencrypted and asynchronous data storage system in React Native that allows users to persist data offline.
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
Hmm. I can’t seem to replicate the issue. Here is the CodeSandbox I’ve experimented with: https://codesandbox.io/s/todos-example-use-local-storage-state-forked-s03chx?file=/src/App.tsx. Can you modify it in order to replicate the problem?
I think your case will be fixed when https://github.com/astoilkov/use-local-storage-state/commit/762fe4f413d5bd0805eab7e8e22bf1b0875d7218 lands in v16. I will write here when that happens so you can test it. How does that sound?