【发布时间】:2017-06-28 11:07:59
【问题描述】:
在组件中我想在调用后立即在 props 中获取数据 webapi服务调用并做一些操作,但问题是它不是 立即更新道具,因为我们知道调用将是 异步,那么解决方案是什么?我在组件中的代码就像 这个:-
openPreviewClick=(event) => {
this.props.GetReport();
console.log(this.props.reportData);
}
function mapStateToProps (allReducers) {
return {reportData: allReducers.reportData
}}
const matchDispatchToProps = (dispatch) => ({
GetReport: () => dispatch(LoadReportData())
})
export default connect(mapStateToProps, matchDispatchToProps)(MyContainer)
现在我必须为此打开一个 pdf,我尝试了两种解决方案:-
- 处理页面的生命周期
componentWillReceiveProps(nextProps) {
if(nextProps.reportPath!=undefined){
window.open(nextProps.reportPath,"thePop","menubar=1,resizable=1,scrollbars=1,status=1,top=280,width=850,height=600");
}
- 在渲染中编写代码
render () {
if(this.props.reportPath!=undefined && this.props.reportPath!=""){}
window.open(this.props.reportPath,"thePop","menubar=1,resizable=1,scrollbars=1,status=1,top=280,width=850,height=600");
}
openPreviewClick 是我要访问的按钮单击 props 命名为 reportData.But console.log(this.props.reportData);是 第一次给我空值,如果我愿意,第二次 单击然后我们正在获取数据。我们如何管理这个?我已经尝试了以上两种解决方案,但它不起作用。
【问题讨论】:
-
在数据可用之前禁用按钮。
-
在获取数据时显示微调器,或渲染为空
标签: reactjs react-redux