Reputation: 313
I want to set state in react native with click on button then set in asyncStorage
and restart to change view but when I use componentDidMount
it not tru and show error
onRadioPressed2 = async() => {
const Newlanguage ='fa';
await AsyncStorage.setItem('language',Newlanguage);
this.setState({language:'fa'});
RestartAndroid.restart();
};
this my function for setState
componentDidMount = () => AsyncStorage.getItem('language').then((Newlanguage) => this.setState({ lang: Newlanguage }));
in another page check in ComponentdidMount but going error .any one can help me.in simulator its ok but in release app not true
Upvotes: 1
Views: 1836
Reputation: 5186
You can use below to add data into AsyncStorage storage.
onRadioPress2() {
try {
const Newlanguage = 'fa';
AsyncStorage.setItem('language', Newlanguage)
this.setState({ language: 'fa' });
RestartAndroid.restart();
} catch (error) {
}
}
You can call the function link:
this.onRadioPress2()
You can retrieve value using the below code:
AsyncStorage.getItem('language').then((Newlanguage) => {
this.setState({
language: Newlanguage
})
}).done();
Your mistake is you are using the wrong name while setting the state.
this.setState({ lang: Newlanguage }) --> this.setState({ language: Newlanguage })
Upvotes: 1