'React native async storage issues
In my react native app I am trying to save data into my local storage. It works fine most of the time but sometime I get error while setting the item.
So here is the code for me setting the item
async setString(key: string, value: any) {
try {
return await AsyncStorage.setItem(key, value)
} catch (e) {
LogService.logError(
'apiClient',
'apptype',
`Error setting local storage, key: ${key} error: ${JSON.stringify(e)}`,
)
}
}
- One of the thing is that my error doesn't prints.
- IOS users are having this issues
- Do I need to first delete the item and then write to it? Doesn't setString override the value?
- Is there any storage problem on the client?
Solution 1:[1]
Try using JSON.parse()
when getting and JSON.stringify()
when setting it
Solution 2:[2]
It's worked for me, you can try this.
useEffect(() => {
AsyncStorage.getItem('isLaunched').then((value) => {
AsyncStorage.setItem('isLaunched', 'true'); // No need to wait for `setItem` to finish, although you might want to handle errors
}); // Add some error handling, also you can simply do
}, []);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Nooruddin Lakhani |
Solution 2 | Siddhartha Mukherjee |