我正在尝试传递values
和props
,该formik需要1个组件。我将各种小组件用于某些形式,并将它们传递到一个复杂的组件中,该组件需要在调用时传递给每个单独的渲染器。
基本上所有FormikProps。这是一个这样的组件。
import React, { Fragment } from 'react';
import debounce from 'debounce-promise';
import classNames from 'classnames';
import { Field, FormikProps, ErrorMessage } from 'formik';
import Asterisk from 'shared/common/components/element/Asterisk';
import { validateUsername } from '../../utils/index';
interface IValues {
username?: string;
email?: string;
}
export const InfoFields = (props: FormikProps<IValues>): JSX.Element => {
const debounceUsernameValidation = (): void => {
debounce(validateUsername, 500);
};
const { touched, errors } = props;
return (
<Fragment>
<div className="pb-2">
<label className="font-weight-bold" htmlFor="username">
Username <Asterisk />
</label>
<Field
validate={debounceUsernameValidation}
className={classNames('form-control', {
'is-invalid': errors.username && touched.username
})}
placeholder="Username (Required)"
autoComplete="username"
name="username"
type="text"
/>
<ErrorMessage name="username" component="div" className="text-danger" />
</div>
<div className="py-2">
<label className="font-weight-bold">Email</label>
<Field
className={classNames('form-control', {
'is-invalid': errors.email && touched.email
})}
autoComplete="email"
placeholder="Email"
name="email"
type="email"
/>
<ErrorMessage name="email" component="div" className="text-danger" />
</div>
</Fragment>
);
};
export default InfoFields;
在这里,我在复杂的组件内部调用它:
render(): ReactNode {
const { mode } = this.props;
return (
<Formik
initialValues={this.getInitialValues()}
validationSchema={this.getValidationSchemas()}
onSubmit={this.handleSubmit}
validateOnBlur={false}
render={({ status, isSubmitting }) =>
(
<Form>
{status && (
<div className="mb-3 text-danger" data-test="user-form-error-message">
{status}
</div>
)}
{mode === ActionMode.ADD_USER && (
<Fragment>
<InfoFields /> // This is the component from above
</Fragment>
)}
<Button
className="btn btn-primary w-100 mt-5"
disabled={isSubmitting}
loading={isSubmitting}
type="submit"
>
{mode === ActionMode.ADD_USER && <span>CREATE USER</span>}
</Button>
</Form>
) as ReactNode
}
/>
);
}
现在,当我在另一个组件中调用该组件时,会出现此错误:
Uncaught TypeError: Cannot read property 'username' of undefined. It refers to this line of code:
errors.username && touched.username and this
errors.email && touched.email
interface IProps {
doSubmit(service: object, values: object): LensesHttpResponse<string>;
onSave(values: { username?: string; email?: string; password?: string; group?: string }): void;
notify(config: object): void;
mode: ActionMode;
user: IUser;
}
interface IValues extends FormikValues {
username?: string;
email?: string;
password?: string;
group?: string;
}
我需要一种将道具username, email
和其他道具传递给每个单独组件的方法。问题在于该组件被称为2渲染,因此它无法访问
我在这里很茫然。有人能帮我吗?谢谢!
答案 0 :(得分:1)
尽管您已经编写了接受Category1, Category2
的组件,但实际上并没有将道具传递给InfoFields
。您可以像这样传递Formik的道具:
FormikProps<IValues>
或者(根据我的个人喜好),删除<Formik
render={formikProps => (
<Form>
// Other Code.
<InfoFields {...formikProps} />
// Other Code.
</Form>
)}
/>
的道具并将InfoField
用作渲染道具,例如:
Field
使用字段渲染道具,您可以访问嵌套在更下方的组件中的表单值,而无需在整个地方传递道具。