您好我是第一次使用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));
任何redux-form专家都可以帮助指导我以正确的方式处理提交的值,以便最终将它们发送到我的API。感谢
更新:工作代码:
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() {
return (
<form onSubmit={this.props.handleSubmit(this.handleSubmit.bind(this))}>
<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));
答案 0 :(得分:9)
Redux-Form用handleSubmit
道具装饰您的组件。根据文档,它是:
一个函数意味着传递给
<form onSubmit={handleSubmit}>
或。{<button onClick={handleSubmit}>
。它将运行验证,同步和 async,如果表单有效,它将调用this.props.onSubmit(data)
包含表单数据的内容。或者,您也可以将
onSubmit
功能传递给handleSubmit
这将取代onSubmit
道具。例如:
因此,如果您的组件没有onSubmit
属性,则必须“手动”将提交处理程序传递给handleSubmit
函数。请试试这个:
<form onSubmit={this.props.handleSubmit(this.handleSubmit.bind(this))}>
请不要将您的handleSubmit
方法与Redux-Form传递的道具混淆为同名。
答案 1 :(得分:0)
(代表问题作者发布的解决方案,将其移至答案空间)。
工作代码:
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() {
return (
<form onSubmit={this.props.handleSubmit(this.handleSubmit.bind(this))}>
<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));