更改为异步存储后如何保存和更新应用程序
how to save and update the application after changing to asyncstorage
render(){
const { navigate } = this.props.navigation;
if (this.state.isRefreshing) {
return(
<Spinner/>
)
}
return(
<Container style={style.container}>
<Content>
<List dataArray={this.state.list} renderRow={(data) =>
<ListItem>
<CheckBox onPress={() => this.saveLang(data.code)} checked={data.checked} />
<Text style={style.black}> {data.title}</Text>
</ListItem>
}/>
</Content>
</Container>
)}
保存后如何更新状态和应用所选语言
saveLang(value){
AsyncStorage.setItem("language", value);
this.setState(
{
list: [
{
'code': 'ru',
'title': 'Русский',
'checked': value == 'ru'
},
{
'code': 'en',
'title': 'English ',
'checked': value == 'en'
}
],
isRefreshing: true,
}
);
I18n.locale = value;}
刷新通过,但不知如何在后台更新应用
enter image description here
很难说不了解你想要发生什么,但是 AsyncStorage.setItem
returns a Promise
所以如果你需要在完成后做某事,只需使用then
:
function saveLang(value) {
AsyncStorage.setItem("language", value).then(() => {
// Perform your refresh
});
}
或者,使用 async
/await
语法:
async function saveLang(value) {
await AsyncStorage.setItem("language", value);
// Perform your refresh
}
render(){
const { navigate } = this.props.navigation;
if (this.state.isRefreshing) {
return(
<Spinner/>
)
}
return(
<Container style={style.container}>
<Content>
<List dataArray={this.state.list} renderRow={(data) =>
<ListItem>
<CheckBox onPress={() => this.saveLang(data.code)} checked={data.checked} />
<Text style={style.black}> {data.title}</Text>
</ListItem>
}/>
</Content>
</Container>
)}
保存后如何更新状态和应用所选语言
saveLang(value){
AsyncStorage.setItem("language", value);
this.setState(
{
list: [
{
'code': 'ru',
'title': 'Русский',
'checked': value == 'ru'
},
{
'code': 'en',
'title': 'English ',
'checked': value == 'en'
}
],
isRefreshing: true,
}
);
I18n.locale = value;}
刷新通过,但不知如何在后台更新应用 enter image description here
很难说不了解你想要发生什么,但是 AsyncStorage.setItem
returns a Promise
所以如果你需要在完成后做某事,只需使用then
:
function saveLang(value) {
AsyncStorage.setItem("language", value).then(() => {
// Perform your refresh
});
}
或者,使用 async
/await
语法:
async function saveLang(value) {
await AsyncStorage.setItem("language", value);
// Perform your refresh
}