[英]React Native - Array Objects to AsyncStorage
只是一個簡單的問題,我一直嘗試使用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);
}
}
更新:
<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)}
/>
通過調用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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.