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`}
/>
添加回答
舉報