2017-05-26 59 views
0

Redux的表單版本: 6.6.3終極版-形式調用備用從ComponentWillRecieveProps提交

反應版本: 15.5.0

我想調用不同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,它很快就返回調用指定的提交功能。

+0

需要查看'this.props.handleSubmit'函數來調試它。 –

+0

沒有直接關係,但我認爲你的if語句嵌套關閉。在if語句塊之後有兩個結束括號,另一個塊在'componentWillReceiveProps' – Jose

+0

@Jose之外浮動,缺少的}只存在於上面的文章中,而不是在我的實際代碼中。仍然會糾正。 – Dranyar

回答

2

當您傳遞函數時,您將立即調用函數。當您引用提交函數時,函數名稱後面不需要包含()this.updateSubmit().bind(this)應該是this.updateSubmit。你也不需要在這裏綁定。

變化:this.props.handleSubmit(this.updateSubmit().bind(this))

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

+0

謝謝,我在updateSubmit之後犯了put()錯誤。但是,修復後,handleSubmit仍然不會調用函數updateSubmit。 – Dranyar

0

我發現我需要做兩件事情。

  1. 使用以下行調用自定義提交方法:this.props.handleSubmit((values)=> this.submitUpdate(values))()注意最後添加的「()」。
  2. 將此行(以及周圍的if條件)移至componentDidUpdate()方法。我在提交方法中使用道具來決定組件的驗證狀態,並且mapStateToProps中的道具在componentWillReceivePropscomponentWillUpdate之前未分配給this.props。

這是我的新的自動提交電話:

componentDidUpdate() { 

//moved this here so that this.props is updated from nextState 
if (this.props.updateMyConfigState == "ValidationFulfilled" 
      && this.props.updateMyConfigValidationClean) { 

     this.props.handleSubmit((values) => this.submitUpdate(values))();    

} 
} 

我知道,這是驗證了Redux形式的推薦之外,我承認這這可能不是處理的最好方式驗證。我當前的理由是:

  1. 它讓我有多個提交驗證操作(分離藥粥創建和更新用戶操作),其中每個人都有自己的錯誤和警告行爲。特別是,錯誤:禁止操作和WarningsOnly:允許但需要額外的用戶批准。
  2. 將視覺錯誤/警告顯示在gui元素之外的單獨列表中。