Redux-Form调度调用Action Creator,但reducer从不接收Action

时间:2016-10-26 02:22:04

标签: redux react-redux redux-form

在下面的示例中,我使用mapDispatchToPropsonSubmit事件绑定到save() Action Creator。这有效 - save() Action Creator将消息记录到控制台。

问题在于它后来不会调度'TEST_SAVE'动作 - 即减速器永远不会收到'TEST_SAVE'类型的动作。

我有一个vanilla redux表单,如下所示,但我是redux-form的新手,我想知道我可能做错了什么?

const reducers = {
  // ... your other reducers here ...
  form: formReducer.plugin({
    contact: (state, action) => {  
      switch(action.type) {
        case 'TEST_SAVE':
          return state;
        default:
          return state;
      }
    }
  })
};
const reducer = combineReducers(reducers);
const store = createStore(reducer);


function mapDispatchToProps(dispatch) {
  return {
    onSubmit: (val) => dispatch(actionCreators.save(val)),
  }
}

var actionCreators = {   
  save: (value) => {
    console.log('SAVE action done!');
    return {type: 'TEST_SAVE', value};
  }, 
};

const MyContactForm = connect(
  mapDispatchToProps
)(ContactForm)


<Provider store={store}>
    <MyContactForm />
</Provider>

ContactForm类的摘录:

class ContactForm extends Component {

  render() {
    const { error, handleSubmit, pristine, reset, submitting } = this.props;

    return (
      <form
        onSubmit={handleSubmit}
        >

    ....

    )

  }
}

ContactForm = reduxForm({
  form: 'contact' 
})(ContactForm);

2 个答案:

答案 0 :(得分:1)

切勿在减速机内使用break。始终返回状态对象。

答案 1 :(得分:1)

const MyContactForm = connect(
  mapDispatchToProps
)(ContactForm)

connect()的第一个参数是mapStateToProps,而不是mapDispatchToProps。应该是:

const MyContactForm = connect(
  undefined, // <------------- mapStateToProps
  mapDispatchToProps
)(ContactForm)