Redux商店更新未显示在道具中

时间:2017-09-07 20:58:46

标签: reactjs redux react-redux

我创建了一个如下所示的验证操作:

export const validate = (requestObject, validationObject) => {

   return (dispatch) => {

   // First, clear any existing errors
   dispatch(clearErrors());

   // Call validation function
   const response = validateObjectFunction(requestObject, validationObject);

   // Set state for isValid
   dispatch(setIsValid(response.isValid));

   // Set error messages
   if(!response.isValid) dispatch(setErrors(response.errors));

   }
}

const setIsValid = (value) => {
   return {
      type: types.SET_IS_VALID,
      value
   };
}

在我的组件中的处理函数中,我只是调用此验证操作,然后根据isValid的状态调用我的后端API。

handleClickSubmit () {

   // Form the request object for validation
   const request = {
      id: this.props.id,
      name: this.props.name
   };

   // Grab the validation object defined inside the compoenent that contains all validation requirements and call validate action.
   this.props.actions.validate(request, validationsObject);

   // This is where the issue is.
   // Although I know for sure, I hit the reducer and set the state of isValid to TRUE, when I get here the isValid is still FALSE
   if(this.props.isValid) this.props.actions.callApiFunction(request);
}

在我的测试中,我首先点击提交,以便将isValid的状态设置为FALSE。然后,我将数据输入到表单中的所有必填字段,然后再次点击提交。

当代码执行时,我看到我点击了reducer,并在点击isValid

行之前将if(this.props.isValid) this.props.actions.callApiFunction(request);设置为TRUE

对我来说,提及我在此过程中没有任何async非常重要。一切都在记忆中发生。

任何想法可能导致isValid的状态不能足够快地更新。

1 个答案:

答案 0 :(得分:0)

在redux生命周期中,道具将不会更新。我假设你在mapStateToProps中设置道具?

在调用validate函数后,你可能会将值从状态中取出,但我可能会建议将其移动到动作创建器而不是组件中。

相关问题