我正在尝试第一次使用redux-form。我可以呈现表单,但无法处理提交。虽然我最终希望将数据发送到服务器,但此时,我只是在尝试控制台记录表单字段值。我收到错误消息:
Error: You must either pass handleSubmit() an onSubmit function or pass onSubmit as a prop
这是我的Profile.jsx文件

import React, {Component} from 'react';
import {connect} from 'react-redux';
import {withAuth} from 'react-devise';
import { Field, reduxForm } from 'redux-form';

class Profile extends Component {
  handleSubmit(data) {
     console.log('Submission received!', data);
   }
  render() {
    const { handleSubmit } = this.props;
    return (
      <form onSubmit={handleSubmit}>
        <div>
          <label htmlFor="firstName">First Name</label>
          <Field name="firstName" component="input" type="text"/>
        </div>
        <div>
          <label htmlFor="lastName">Last Name</label>
          <Field name="lastName" component="input" type="text"/>
        </div>
        <div>
          <label htmlFor="email">Email</label>
          <Field name="email" component="input" type="email"/>
        </div>
        <button type="submit">Submit</button>
      </form>
    );
  }
}

// Decorate the form component
Profile = reduxForm({
  form: 'profile' // a unique name for this form
})(Profile);


const mapStateToProps = state => {
  return {
    currentUser: state.currentUser
  };
};

export default connect(mapStateToProps)(withAuth(Profile));

如何处理提交的值,最终将它们发送到我的API?

最佳答案

Redux-Form使用handleSubmit属性装饰您的组件。根据文档,它是:



因此,如果您的组件没有onSubmit属性,则必须“手动”将提交处理程序传递给handleSubmit函数。请尝试以下方法:

<form onSubmit={this.props.handleSubmit(this.handleSubmit.bind(this))}>

请不要将handleSubmit方法与从Redux-Form传递的具有相同名称的prop混淆。

09-18 10:14