React Native - 将数组对象转换为AsyncStorage

时间:2018-06-16 18:27:09

标签: javascript android react-native asynchronous

只是一个简单的问题,我一直试图用AsyncStorage制作一个项目列表,但我无法理解。我尝试在网上的一些博客上尝试一些尝试,但它不起作用。所以,我跑到这里,希望有人可以帮助我。

这是我的代码:

  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 个答案:

答案 0 :(得分:0)

从调用[ {"title": "ExampleTitle1", "author": "Username", "score": "11000"}, {"title": "ExampleTitle2", "author": "Username2", "score": "12000"}, {"title": "ExampleTitle3", "author": "Username3", "score": "13000"}, {"title": "ExampleTitle4", "author": "Username4", "score": "9000"}, .... ] 对象或数组获得的值是多少?如果它只是一个对象,看起来您可能希望将其附加到[ {"name": "Username", "karma": "00000"}, {"name": "Username2", "karma": "00000"}, {"name": "Username3", "karma": "00000"}, .... ] 数组。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destCtrl = segue.destination as! SecondTableViewController
        destCtrl.hidesBottomBarWhenPushed = false
        destCtrl.testLabelText = "hello"
}