componentDidMount() {
this.props.fetchAccountData();
}
render() {
const { accounts } = this.props.accountData;
policyDetailsInfo = accounts[0].policy_summary;
accountDetails = accounts[0];
return (
<div>
<Account
accountDetails={accountDetails}
policyDetails={policyDetailsInfo}
/>
</div>
)
}
const mapStateToProps = state => ({
accountData: state.accountData
});
const mapDispatchToProps = {
fetchAccountData
};
export default connect(mapStateToProps, mapDispatchToProps)(AccountHome);
此致
答案 0 :(得分:0)
如果您正确配置了reducer,每当异步调用响应到达时,render()
方法AccountHome
组件将被触发(因为accountData
发生了变化,状态为AccountHome
已注册)
const mapStateToProps = state => ({
accountData: state.accountData
})
您可以通过多种方式编写处理accountData的逻辑,但为了简单起见并专注于您粘贴的代码,请更新render()
方法并让其了解accountData prop
:
render() {
const accounts = this.props.accountData;
if(!accounts || !accounts.length ) {
// set a simple loading solution
// avoid errors using empty accounts
return (
<div>
Loading content...
</div>
}
}
// here accounts has data inside
const policyDetailsInfo = accounts[0].policy_summary;
const accountDetails = accounts[0];
return (
<div>
<Account
accountDetails={accountDetails}
policyDetails={policyDetailsInfo}
/>
</div>
}
}