AsyncStorage not working properly
See original GitHub issueCurrent behavior
AsyncStorage not working properly on 0.60.5 I set and get the async storage with the help of two button. Value successfully sets and gets but when i reload the application and tried to get the value again without setting it return null.
Expected behavior
Async save the set value before and don’t remove it until we manually set it to null. But now every time application is reloaded it forgets the saved async value and i have to set it again and again. Basically setup my authentication with asyncstorage, if user is signed in it shows dashboard else login screen. But now user login and open app again, it shows login screen again and again. It was working fine on Android 8.1 with React Native 0.59. But i upgraded my project to 0.60, its behavior changed.
Code
export default class LibTest extends Component {
static navigationOption = {
title: "LibTest", header: null, headerVisible: false
};
constructor(props) {
super(props);
this.state = {
asyncVal: null,
storedData: "myValue"
}
}
async componentDidMount() {
}
storeData = async () => {
console.log("inside storeData")
try {
await AsyncStorage.setItem('Test', 'TestValue')
} catch (e) {
console.log(e)
}
}
getData = async () => {
console.log("inside getData")
try {
const value = await AsyncStorage.getItem('Test')
this.setState({ storedData: value })
} catch (e) {
// error reading value
}
}
render() {
return(
<View style={{ marginTop: 40 }}>
<Text> {this.state.storedData}</Text>
<Button title={"storeData"} onPress={this.storeData}><Text>storeData</Text></Button>
<Button title={"getData"} onPress={this.getData}><Text>getData</Text></Button>
</View>
)
}
}
Environment
- Async Storage version: 1.6.1
- React-Native version: 0.60.5
- Platform tested: Android 9
Issue Analytics
- State:
- Created 4 years ago
- Reactions:9
- Comments:31 (9 by maintainers)
Top Results From Across the Web
Async storage not working to set or get on React Native
Im trying to make the user authentication using async storage to decide which screen will be rendered so when i get the ...
Read more >AsyncStorage - React Native
AsyncStorage is an unencrypted, asynchronous, persistent, key-value storage system that is global to the app. It should be used instead of ...
Read more >How To Use React Native AsyncStorage | Jscrambler - Medium
AsyncStorage is a simple, asynchronous, unencrypted by default module that allows you to persist data offline in React Native apps. Learn more about...
Read more >React Native AsyncStorage - Asap developers
This proves it's really working. Now that we know it works, it's time to store and use our application theme properly. Replace your...
Read more >Building offline React Native apps with AsyncStorage
An end-to-end tutorial showing how to build an offline to-do list ... Since there's no data stored right now, the text after the...
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
@Krizzu I found bug in my code, thanks for your help, and sorry for trouble. The problem was that I checked the state in one of my Context which was not yet updated with the value stored in AsyncStorage while first rendered.
Setting values to
null
/undefined
is not supported - probably the error is thrown there, which you don’t handle. If you want to remove value, use.removeItem
method