我正在从后端获取数据,以这种字体显示
componentDidMount() {
const response = this.props.store.privateImputationData;
console.log(response);
}
它在控制台中显示为空,现在如果我执行setTimeout
,它就可以工作了!
componentDidMount() {
setTimeOut(() => {
const response = this.props.store.privateImputationData;
console.log(response);
}, 500);
}
这是我从商店中获取数据的方式:
@computed get ImputationData() {
return this.privateImputationData || {};
}
loadImputation = (diplayedImputations) => {
HttpClient.postJSON(this.apiDataUrl, diplayedImputations).then((result) => {
this.privateImputationData = result;
this.loadAdditionalData();
});
}
如果没有setTimeout
怎么办?
答案 0 :(得分:0)
您可以使用状态对象:State and Lifecycle。每当状态改变时,无论使用什么组件,都会对其进行更新。
this.state = {privateImputationData: null} //or some default
所以在您的代码中:
@computed get ImputationData() {
return this.privateImputationData || {};
}
loadImputation = (diplayedImputations) => {
HttpClient.postJSON(this.apiDataUrl, diplayedImputations).then((result) => {
this.setState({privateImputationData: result});
this.loadAdditionalData();
});
}
要使用值:
this.state.privateImputationData;