代码之家  ›  专栏  ›  技术社区  ›  Violet

将本机数组对象响应到异步存储

  •  0
  • Violet  · 技术社区  · 6 年前

    只是一个简单的问题,我一直试图用异步存储创建一个项目列表,但我就是想不通。我以前在网上的一些博客上尝试过一些尝试,但是没有成功。所以,我跑到这里希望有人能帮我。

    这是我的代码:

      constructor(props) {
        super(props);
        this.state = {
            myKey: []
        }
      }
    
      async getKey() {
        try {
          const value = await AsyncStorage.getItem('@MySuperStore:key');
          this.setState({myKey: value});
        } catch (error) {
          console.log("Error retrieving data" + error);
        }
      }
    
      async saveKey(value) {
        try {
          await AsyncStorage.setItem('@MySuperStore:key', value);
        } catch (error) {
          console.log("Error saving data" + error);
        }
      }
    

    更新:

    Warning_ScreenShot

        <TextInput
          style={styles.formInput}
          placeholder="Enter key you want to save!"  // This is the component where I think the warning came from. 
          value={this.state.myKey}
          onChangeText={(value) => this.saveKey(value)}
        />
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   topic    6 年前

    你打电话的价值 AsyncStorage.getItem('@MySuperStore:key') 一个物体还是一个数组?如果它只是一个对象,那么看起来您可能希望将它附加到 myKey 数组。

    async getKey() {
      try {
        const value = await AsyncStorage.getItem('@MySuperStore:key');
        const myKey = [ ...this.state.myKey, value ];
        this.setState({ myKey });
        } catch (error) {
        console.log("Error retrieving data" + error);
      }
    }