当我尝试在 React Native 中使用 AsyncStorage 保存值时,但当我尝试检索该值时,我有 {"_U": 0, "_V": 0, "_W": null, "_X": null}
When i try to save a value with AsyncStorage in React Native, but when i try to retrieve the value, i have {"_U": 0, "_V": 0, "_W": null, "_X": null}
我的按钮代码是:
<Button style={styles.shopButtonStyle} onPress={async () => { await this.setState({'selectedShop': negozio['@id'] }); await AsyncStorage.setItem('@shop', "test"); }}>
<Text>Scegli</Text>
</Button>
然后我转到另一个页面:
<Button style={styles.shopButtonStyle} onPress={async () => { this.props.navigation.navigate('Bookingstep2');
}}>
<Text>Continua</Text>
</Button>
在新页面我有:
componentDidMount(){
async () => { const shop = await AsyncStorage.getItem('@shop'); console.log(shop); }
}
但是控制台是空的。谁能帮帮我?
您应该让您的 componentDidMount 异步,并使用 try catch 来检查错误,例如:
async componentDidMount() {
try {
const shop = await AsyncStorage.getItem('@shop');
console.log(shop);
} catch (error) {
console.log(error);
}
}
可能有任何错误,您可以在 catch 块中检查错误
我的按钮代码是:
<Button style={styles.shopButtonStyle} onPress={async () => { await this.setState({'selectedShop': negozio['@id'] }); await AsyncStorage.setItem('@shop', "test"); }}>
<Text>Scegli</Text>
</Button>
然后我转到另一个页面:
<Button style={styles.shopButtonStyle} onPress={async () => { this.props.navigation.navigate('Bookingstep2');
}}>
<Text>Continua</Text>
</Button>
在新页面我有:
componentDidMount(){
async () => { const shop = await AsyncStorage.getItem('@shop'); console.log(shop); }
}
但是控制台是空的。谁能帮帮我?
您应该让您的 componentDidMount 异步,并使用 try catch 来检查错误,例如:
async componentDidMount() {
try {
const shop = await AsyncStorage.getItem('@shop');
console.log(shop);
} catch (error) {
console.log(error);
}
}
可能有任何错误,您可以在 catch 块中检查错误