2 回答

TA贡献1895条经验 获得超7个赞
这是一个很好的解决方案,本质上是你的,但你可以提供任何东西,而不仅仅是标签。创建字段并将标签放在类似内容上,以便:
<Field name={`patients[${index}].firstName`} label='First Name' component={MuiTextFieldFormik} />
诀窍是使用点差运算符,然后自定义组件变为:
import React from 'react';
import { TextField } from "@material-ui/core"
import { get } from "lodash"
export const MuiTextFieldFormik = ({ field, form: { touched, errors }, ...props }) => {
const error = get(touched, field.name) && !!get(errors, field.name)
const helperText = get(touched, field.name) && get(errors, field.name)
return (
<TextField fullWidth variant="outlined" {...field} {...props} error={error} helperText={helperText} />
)
}
令人失望的是,他们的文档没有这么简单的例子

TA贡献1877条经验 获得超6个赞
好吧,所以我想我找到了解决方案。我破坏参数的方式是,我只是传递字段和表单,这些字段和表单保存了大部分数据,因此以这种方式传递标签道具可以修复:
const Input = ({ field, label, form: { errors } }) => {
const errorMessage = getIn(errors, field.name);
return <TextField {...field} label={label} />;
};
然后,当我以这种方式使用 Formik 组件时,正确的标签被传递:
<Field
label="first name"
component={Input}
name={`patients[${index}].firstName`}
/>
添加回答
举报