Reactjs 如果通过此.props.submit(';formname';)调用submit,则onSubmit处理程序出错

Reactjs 如果通过此.props.submit(';formname';)调用submit,则onSubmit处理程序出错,reactjs,redux-form,Reactjs,Redux Form,当从Redux表单提供的操作创建者调用onSubmit处理程序时,我在Redux表单中遇到错误。如果我在里面用一个普通的按钮,它就会正常工作 import { Field, reduxForm } from 'redux-form'; class Property extends Component { constructor(props) { super(props); this.saveOnChange = this.saveOnChange.bi

当从Redux表单提供的操作创建者调用onSubmit处理程序时,我在Redux表单中遇到错误。如果我在里面用一个普通的按钮,它就会正常工作

import { Field, reduxForm } from 'redux-form';

class Property extends Component {
    constructor(props) {
        super(props);

        this.saveOnChange = this.saveOnChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
    }

    handleSubmit() {
        console.log('form submitting');
    }

    saveOnChange() {
        console.log('should auto save');
        this.props.dispatch(this.props.submit('propertySettings'));
    }

    render() {
        const { handleSubmit } = this.props;

        return(
            <form onSubmit={handleSubmit(this.handleSubmit)}>
                // Stuff goes here

                <div onClick={this.saveOnChange}>(just for example it is with a div)</div> // <-- Get's errors
                <button type='submit'>Via button</button> // <-- Work's fine
            </form>
        );
    }
}
从'redux form'导入{Field,reduxForm};
类属性扩展组件{
建造师(道具){
超级(道具);
this.saveOnChange=this.saveOnChange.bind(this);
this.handleSubmit=this.handleSubmit.bind(this);
}
handleSubmit(){
console.log(“表单提交”);
}
saveOnChange(){
log('should auto save');
this.props.dispatch(this.props.submit('propertySettings');
}
render(){
const{handleSubmit}=this.props;
返回(
//这里有东西

(仅举一个div为例)//似乎您试图将
handleSubmit
的本地方法绑定到组件的范围,然后将其作为参数传递给props上的
handleSubmit
方法

如果您只是尝试将handleSubmit方法分配给表单的onSubmit,则可以尝试以下操作:

import { Field, reduxForm } from 'redux-form';

class Property extends Component {
  constructor(props){
    super(props);
    this.handleSubmit = this.handleSubmit.bind(this);
    // bind handleSubmit in the constructor, it's more efficient
    // as it only occurs once per lifetime of a component
  }
  handleSubmit() {
    console.log('form submitting');
  }
  render() {
    return(
      <form onSubmit= { this.handleSubmit }> // call it here like this
      // Stuff goes here
      </form>
    );
  }
}
从'redux form'导入{Field,reduxForm};
类属性扩展组件{
建造师(道具){
超级(道具);
this.handleSubmit=this.handleSubmit.bind(this);
//在构造函数中绑定handleSubmit,效率更高
//因为它在组件的每个生命周期中只发生一次
}
handleSubmit(){
console.log(“表单提交”);
}
render(){
返回(
//像这样叫这里
//这里有东西
);
}

}
从版本redux form 6.3.x更新到版本redux form 6.4.x解决了问题。请参阅。

将行更改为@therewillbecode如何查看更新的答案,此错误仅在使用redux formI提供的操作创建者调用submit时存在。我发现只有在使用正在调用操作创建者以调用提交。请参阅更新的问题您仍在以不同于我建议的方式分配handle submit。是的,但这只是因为handleSubmit是处理提交所需的redux道具表单。请参阅解决我的问题的我的答案:)谢谢您的帮助!