【发布时间】:2017-12-11 05:33:08
【问题描述】:
我正在尝试从异步存储中获取值并将其分配给状态。 访问 URL 时,状态值变为空/未定义
constructor(props) {
super(props);
this.state = {
stAccntList: [],
stUserAccountNo: '',
stCustNo: '',
resp: '',
};
AsyncStorage.getItem('UserAccountNo').then((value) => {
if (value != '') {
accno = value;
this.setState({ stUserAccountNo: value });
// alert(this.state.stUserAccountNo);
}
}).done();
AsyncStorage.getItem('CustomerNo').then((value) => {
if (value != '') {
cusno = value;
this.setState({ stCustNo: value });
// alert(this.state.stUserAccountNo);
}
}).done();
}
componentWillMount() {
let restUrl = urls.URL + this.state.stCustNo + "/" + "accounts" + "/" + this.state.stUserAccountNo;
}
this.state.CustNo 和 this.state.stUserAccountNo 返回 null 。
请告诉我哪里出了问题。
【问题讨论】:
-
由于您尝试异步获取数据并更新状态,因此不能保证在 componentWillMount 执行之前数据可用。
-
如何实现?
-
取决于在componentWillMount中restUrl的目的是什么
-
我会用那个 restUrl 打 API
标签: javascript reactjs react-native ecmascript-6