没有链接承诺的异步操作

时间:2018-03-25 01:42:14

标签: javascript reactjs redux redux-thunk

使用componentWillReceiveProps和redux异步操作时遇到问题,下面是我的代码和说明。

componentWillReceiveProps(nextProps) {
  if(nextProps.job.status === 'applied'){
    this.showAppliedDialog()
  }
}

componentDidMount() {
  this.props.fetchJob(this.props.match.params.id)
}

要求: 当用户点击应用按钮时 - 将用户的工作状态更改为“已应用” - 显示“感谢您申请”的对话框以通知用户

错误: 应用用户的工作状态后,他重新访问路线或刷新当前路线, 显示“谢谢您的申请”对话框。

预期: 不要显示“谢谢你申请”对话框

我用回调方法解决了上述问题。但这是唯一的方法吗?我想用减速机代替 继续返回我的异步调度并以回调方式执行

componentDidMount() {
  this.props.fetchJob(this.props.match.params.id)
  .then(resp => resp.status === 'applied' && this.showAppliedDialog())
}

1 个答案:

答案 0 :(得分:0)

你可以这样做。

componentWillReceiveProps(nextProps) {
  if(nextProps.job.status === 'applied' && this.props.job.status !== 'applied'){
    this.showAppliedDialog()
  }
}

通过执行此操作,只有在将状态更改为“已应用”时才会显示对话框,如果已应用状态,则不会显示对话框。

相关问题