gpt4 book ai didi

javascript - AsyncStorage 获取项目

转载 作者:行者123 更新时间:2023-11-29 19:00:56 27 4
gpt4 key购买 nike

如何在“componentDidMount”处获取 AsyncStorage 项目,这里是我的代码

componentDidMount() {

AsyncStorage.getItem('customer_id').then((value)=> this.setState({ customer_id: value }));
console.log(this.state.customer_id); /* doesn't work */
console.log(`${this.state.customer_id}`); /* doesn't work */
}


render() {return <View><Text>Customer ID : {this.state.customer_id}</Text></View>; /* it's work */ }

最佳答案

您必须处理 AsyncStorage - 异步,这很有趣!无论如何,您想将回调传递给处理结果的 getItem 方法。尝试

componentDidMount(){
AsyncStorage.getItem('customer_id', (error,value) => {
if (!error) { //If there are no errors
//handle result
if (result !== null) this.setState({customer_id:value});
}
});
}

关于javascript - AsyncStorage 获取项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46945039/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com