在哪里存储redux-form成功消息,以及如何在onSubmitSuccess之后显示它?

时间:2017-01-09 17:06:45

标签: reactjs react-redux redux-form

我一直在阅读有关该主题的文档和几个github问题,但我无法找到关于如何处理简单成功消息的明确答案,该消息由服务器返回并显示给用户。

我的reduxForm缩减器看起来像这样:

form: formReducer.plugin({
    forgotPassword: (state, action) => { // <------ 'forgotPassword' is name of form given to reduxForm()
      switch(action.type) {
        case 'FORGOT_PASSWORD_EMAIL_SENT': {
         return {
            ...state,
            successMessage: action.payload
          };
        }
        default:
          return state;
      }
    }
  })

在我的实际表单组件中,我有这个:

.......
onSubmitSuccess: (successMessage, dispatch) => { dispatch({type: 'FORGOT_PASSWORD_EMAIL_SENT', payload: successMessage}) }

// now we connect the component to the Redux store:
function mapStateToProps(state) {
  return {
    appcoreData: state.appcoreData
  };
}

function mapDispatchToProps(dispatch) {
  return bindActionCreators({
                              doForgotPassword: doForgotPassword
                            }
                            , dispatch);
}

// Decorate the form component
ForgotPassword = reduxForm(formConfig)(ForgotPassword);
ForgotPassword = connect(mapStateToProps, mapDispatchToProps)(ForgotPassword);

export default ForgotPassword;

现在,当表单成功提交后,所有内容都会正确执行,我的状态将会显示为 state.form.forgotPassword.successMessage =&#39; my string&#39; ;但组件永远不会更新以显示实际消息。

也就是说在我的Form组件中,我应该如何访问该州的这一部分?它不作为财产传承下来,所以我无法做到

const {successMessage} = this.props;

在控制台中,我可以看到调度的动作如下:

@@redux-form/SET_SUBMIT_SUCCEDED
FORGOT_PASSWORD_EMAIL_SENT

因此,从我的表单组件中,访问state.form.forgotPassword.successMessage并将其显示给用户的正确方法是什么?

1 个答案:

答案 0 :(得分:-2)

尝试使用formValueSelector()。 请参阅此处的示例:https://redux-form.com/6.6.0/examples/selectingFormValues/