0

我有两个按钮,我想将样式保存在异步存储中。目前它们被保存在这样的状态:

 state = {
  itemOne:styles.button,
  itemTwo:styles.button,
}

并且根据值的样式改变

  if (value) {
                switch (value) {
                    case "Low":
                        this.setState({itemOne: styles.styleOne});
                        break;
                    case "Moderate":
                        this.setState({itemOne: styles.styleTwo});
                        break;

                }
            }

视图中的按钮看起来像这样

 <Button
                        style={this.state.itemOne}
                        onPress={() => {
                            this.popupDialog.show();
                        }}
                    >

我正在使用 AsyncStorage 来保存状态,但它出现了这个错误

 Warning: Failed prop type: Invalid props.style key `0` supplied to `View`.
Bad object: {
  "0": "6",
  "1": "9",
  "opacity": 1
}

请帮忙!

4

1 回答 1

3

您可以使用 JSON 序列化/反序列化保存对象。您是如何在 AsyncStorage 上存储样式对象的?你能添加你的 AsyncStorage 代码吗?如果你得到并设置这样。它将被修复

await AsyncStorage.setItem("myStyle", JSON.stringify(style)); //for set

var style = JSON.parse(await AsyncStorage.getItem("myStyle"));
于 2018-05-09T21:29:24.047 回答