gpt4 book ai didi

reactjs - Redux 形式的 FieldArray,handleSubmit' 未定义

转载 作者:行者123 更新时间:2023-12-03 14:01:21 24 4
gpt4 key购买 nike

我正在努力使用 redux 形式的 FieldArray。我在将表单与实际的 react 组件连接起来时遇到了挑战。当我尝试提交表单时,出现以下错误:错误“handleSubmit”未定义

文件如下。我是否在我的 React 组件中正确构建了这个表单?知道为什么handleSubmit 出错吗?

import React from 'react';
import {Field, FieldArray, reduxForm} from 'redux-form'
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import * as inviteActions from '../../actions/inviteActions';

let renderEmailField = ({input, label, type, meta: {touched, error}}) =>
<div>
<label>{label}</label>
<div>
<input {...input} name="email" type="email"/>
{touched && error && <span>{error}</span>}
</div>
</div>

let renderEmails = ({fields, meta: {submitFailed, error}}) =>
<ul>
<li>
<button type="button" onClick={() => fields.push()}>Add Email</button>
</li>
{fields.map((email, index) =>
<li key={index}>
{index > 2 && <button
type="button"
title="Remove Email"
onClick={() => fields.remove(index)}
/>}
<Field
name={email}
component={renderEmailField}
label={`Email #${index + 1}`}
/>
</li>
)}
{submitFailed && error && <li className="error">{error}</li>}
</ul>

let EmailsForm = ({handleSubmit, pristine, reset, submitting}) =>
<form onSubmit={handleSubmit}>
<FieldArray name="emails" component={renderEmails} />
<div>
<button type="submit" disabled={submitting}>Submit</button>
<button type="button" disabled={pristine || submitting} onClick={reset}>
Clear Values
</button>
</div>
</form>


class InvitePage extends React.Component {

handleSubmit(data) {
console.log(data)
this.props.actions.createInvites(data);
}

render() {

return (
<div>
<h1>Invites</h1>
<EmailsForm onSubmit={handleSubmit}/>
</div>
);
}

}

EmailsForm = reduxForm({
form: 'emailsForm',
initialValues: {
emails: ['', '', '']
},
validate(values) {
const errors = {}
if (!values.emails || !values.emails.length) {
errors.emails = {_error: 'At least one email must be entered'}
}
else {
let emailsArrayErrors = []
values.emails.forEach((email, emailIndex) => {
const emailErrors = {}
if (email == null || !email.trim()) {
emailsArrayErrors[emailIndex] = 'Required'
}
})
if (emailsArrayErrors.length) {
errors.emails = emailsArrayErrors
}
}
return errors
}
})(EmailsForm)

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

function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(inviteActions, dispatch)
};
}

export default connect(mapStateToProps, mapDispatchToProps)(InvitePage);

最佳答案

您尚未提供 onSubmit 函数作为 EmailsForm 的支柱。您可以通过两种方式传递它:

EmailsForm = reduxForm({
form: 'emailsForm',
...
onSubmit: () => {}
...
})(EmailsForm)

或者您可以在调用 EmailsForm 时将 onSubmit 作为 prop 传递

<EmailsForm onSubmit={() => {}} />

就你的情况而言,你必须这样写:

<EmailsForm onSubmit={this.handleSubmit.bind(this)}/>

根据我的说法,如果您可以重复使用这些小组件renderEmailField、renderEmails、EmailsForm,那么您可以像现在一样将它们创建为单独的组件。

我建议,您不应该将 EmailsForm 与 InvitePage 类分开,因为随着您的需求增长,您必须将所有 Prop 从 InvitePage 传递到 EmailsForm。除了传递 onSubmit 函数之外,InvitePage 目前没有任何其他用途。

class InvitePage extends React.Component {

handleSubmit = data => {
console.log(data)
this.props.actions.createInvites(data);
}

render() {
const { pristine, reset, submitting } = this.props
return (
<div>
<h1>Invites</h1>
<form onSubmit={this.handleSubmit}> // react recommends we should not bind function here. Either bind that in constructor or write handleSubmit like I have written.
<FieldArray name="emails" component={renderEmails} />

<div>
<button type="submit" disabled={submitting}>Submit</button>
<button type="button" disabled={pristine || submitting} onClick={reset}>
Clear Values
</button>
</div>
</form>
</div>
)
}

}

InvitePage = reduxForm({
form: 'emailsForm',
initialValues: {
emails: ['', '', ''],
},
validate(values) {
...
}
})(InvitePage)

希望它能起作用。

如果您不更改任何变量的值,请尝试使用 const 而不是 let

关于reactjs - Redux 形式的 FieldArray,handleSubmit' 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44449509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com