retrieveData = async () => {
try {
//Retrieving values from AsyncStorage
let voiture = await AsyncStorage.getItem('VOITURE')
this.state.DBCarName=[];
this.state.DBCarName = JSON.parse(voiture);
alert(this.state.DBCarName)
}
catch {
alert('error')
}
}
The call of the function :
render() {
this.retrieveData();
I want to use my retrieve function in order to assign a value to the DBCarName state in order to render it in a Text component. The thing is, when calling the function in the render method, it returns null, which probably means that my function didn't execute and my array state is empty.
Any idea on how to deal with this?
error. Maybe you are just having a typo withVOITUREor it may have the value null set by default or by any operation.this.state. There issetStatemethod for that. You should not call functions involving side-effects inrender, use corresponding lifecycle method or hooks for that.didMount()and modify your state there.