javascript - Redux-forms 从 ComponentWillRecieveProps 调用备用提交

标签 javascript reactjs redux-form

redux-forms 版本:6.6.3

react 版本:15.5.0

我想从我的 React 组件中的 componentWillRecieveProps 函数调用不同的提交函数。

componentWillReceiveProps(nextProps) {

 if (nextProps.updateTierConfigState == "ValidationFulfilled" 
            && nextProps.updateMyConfigValidationClean) {
  console.log('CWRP calling submit()')
  //this.props.submit(); //THIS CALLS DEFAULT on FORM's onSubmit
  this.props.handleSubmit(this.updateSubmit().bind(this))

 }    
 else {
  this.props.handleSubmit(this.createSubmit().bind(this))
 }
}

updateSubmit(values) { 
  //do stuff
}

createSubmit(values) { 
  //do stuff
}

我见过这样的例子:https://github.com/erikras/redux-form/issues/711#issuecomment-191850962

但是,我无法成功调用handleSubmit。它不会调用传入的函数。

我已经调试到handleSubmit,它返回得非常快,没有调用指定的提交函数。

最佳答案

传入函数后,您将立即调用这些函数。引用提交函数时,不需要在函数名称后包含 ()this.updateSubmit().bind(this) 应该是 this.updateSubmit。您也不需要此处的绑定(bind)。

更改:this.props.handleSubmit(this.updateSubmit().bind(this))

至:this.props.handleSubmit(this.updateSubmit)

关于javascript - Redux-forms 从 ComponentWillRecieveProps 调用备用提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44206746/

相关文章:

php - getElementId 返回 null 或 undefined

javascript - React Router v4.2 风格

reactjs - 如何在 React 中使用 wavesurfer.js?

reactjs - 如何使用 redux-form 进行重新选择

javascript - JavaScript 或 jQuery 中是否有 "copy to clipboard"函数?

javascript - Morris 图表未显示 MVC 的最后一个 xkey 值

javascript - 只有第一个单选按钮的属性检查才能正常工作

javascript - componentWillReceiveProps 不会触发

reactjs - Redux 表单 - 多个更新功能呈现缓慢

javascript - 从 handleSubmit 函数抛出 redux-form 提交错误