我的问题并不是关于将map*ToProps
函数放在哪里,我只是对 redux-form 为您提供了一个dispatch
参数这一事实视而不见,该参数允许您再次绑定动作创建者,纯粹是为了使用正在运行的动作进行验证。
它还需要将函数从装饰器中移出到常量中,就像问题中链接的示例一样。
这是任何感兴趣的人的完整示例:
import React, { Component, PropTypes } from 'react';
import { bindActionCreators } from 'redux';
import { reduxForm } from 'redux-form';
import * as auth from 'actions/auth';
const fields = [
'username',
'password'
];
const asyncValidate = (values, dispatch) => {
return new Promise((resolve, reject) => {
if(values.username) {
let authActions = bindActionCreators(auth, dispatch);
authActions.checkUsernameValid(values.username);
resolve();
}
});
};
@reduxForm(
{
form: 'auth.login',
fields,
asyncValidate,
asyncBlurFields: [ 'username' ]
},
state => ({
usernameValid: state.auth.usernameValid,
usernameValidError: state.auth.usernameValidError
}),
dispatch => ({
authActions: bindActionCreators(auth, dispatch)
})
)
export default class Login extends Component {
// Component here which has access to authActions
// if the form successfully submits.
}