【发布时间】:2017-10-24 17:27:11
【问题描述】:
我需要在 React Native 应用程序中将布尔值存储在 AsyncStorage 中。存储的值将在整个应用程序中使用。实际上,用户将在设置页面中将变量设置为 true 或 false,然后根据该值,我在应用程序的其他页面中进行一些更改。
这是我正在使用的代码 设置页面(设置值为真或假,但默认值为真)
constructor(props) {
super(props);
this.state ={
status: true
}
};
toggleStatus(){
this.setState({
status:!this.state.status
});
// AsyncStorage.setItem('myCheckbox',JSON.stringify(this.state.status));
//I even hardcoded the value stored and set it to false but it didn't work
AsyncStorage.setItem('myCheckbox', JSON.stringify(false));
}
..
<TouchableOpacity onPress={this.toggleStatus.bind(this)}>
<Text> touch me </Text>
</TouchableOpacity>
在我将使用存储在 AsyncStorage 中的值的其他页面中:
componentDidMount() {
const value = AsyncStorage.getItem('myCheckbox', (value) => {
JSON.parse(value)
console.log("my check box value ", value)
});
【问题讨论】:
标签: serialization react-native save boolean async-await