I know the similar question has been asked such as this, but am not able to find an answer to this. I am a newbie to react-native as well. I need to retrieve data from storage this is how I store the value after API call
_storeData = async response => {
it AsyncStorage.setItem(token, (response))
.then(() => {
this.setState({
shouldLoad : false
});
console.log("Data saved successfully" + response);
this.props.navigation.navigate("HomeScreen");
})
.catch(() => {
console.log("There was an error saving the data");
});
};
This is how I tried to retrieve data from AsyncStorage
AsyncStorage.getItem(token).then((token) => {
this.setState({
token : tokentoken,
currentState : false
});
});
What am trying to achieve is session management. On login, I save the sessionToken to AsyncStorage, and when the app is started again, I need to retrieve the data back. I tried different code but am not getting output.
Any help will be appreciated Thanks in advance
EDIT 1.
Am pasting the whole code here so that anyone could point out where am going wrong.
export default class App extends Component {
constructor() {
super();
this.state = {
token: null,
currentState : true,
};
}
componentDidMount() {
AsyncStorage.getItem(token).then((token) => {
this.setState({
token : tokentoken,
currentState : false
});
});
}
render() {
var ggg = this.state.token;
console.log(ggg);
if(this.state.currentState === false){
if (this.state.token === "" || this.state.token === null) {
return <LoginNav />;
} else {
return <HomeNavigatorNew />;
}
}
else{
return ( <ActivityIndicator
animating={true}
style={{
flex: 1,
alignItems: 'center',
justifyContent: 'center',
height: 80
}}
size="large"
/>)
}
}
}
EDIT 2.
I was writing the above code in App.js I just moved the whole code to Login.js(another file) and it just worked. Does that mean anything.. .? Is it like that I should write retreiving code in App.js
EDIT 3. Thanks to @Mohammed Ashfaq and @Samer Murad for responding so quickly. In Sammrs code I noticed the AsyncStorage is imported in curly parenthes not without them. I was importing without them. I had mentioned in my EDIT 2 that the code was working in another file in which the AsyncStorage is impoted in curly paranthes. Leaving the modified working code for future reference for someone
import React, {Component} from "react";
import {AsyncStorage, ActivityIndicator} from "react-native";
import { createStackNavigator} from "react-navigation";
export default class App extends Component {
constructor() {
super();
this.state = {
token: null,
currentState: true,
};
}
_retrieveData = async () => {
console.log("Data retrieved successfully");
// var tokenRetrieved = AsyncStorage.getItem('LoginTocken');
await AsyncStorage.getItem('LoginTocken')
.then(value => {
return (value);
})
.then ( valueJson => {
this.setState({
token : valueJson,
currentState : false
});
console.log("Data retrieved successfully", valueJson);
})
.catch((error) => {
console.log("There was an error retrieving the data" + error);
});
};
handleSession = async () => {
console.log("handleSession");
this._retrieveData().catch((error) => {console.log("error is " +
error);});
};
componentWillMount() {
console.log("componentWillMount");
this.handleSession();
}
render() {
if (this.state.token === "" || this.state.token === null) {
return <LoginNav / > ;
} else {
return <HomeNavigatorNew / > ;
}
}
}
I don't have a clue why and how it make difference when some import are done without curly parenthes**{}** where some are done with them. If anyone know why the curly paranthes are omitted/added at times I request them to leave a answer.