Formik 的表单无法识别 material UI 组件的文本字段值?
Formik's forms don't recognize material UI component's text field value?
我使用 formik 和 material UI 构建了一个简单的联系页面。一切正常,除了当我使用 Material UI 组件而不是常规输入标签时。程序似乎无法读取 Material UI TextField
组件中的值。
这个有效:
<Field
name="name"
id="outlined-textarea"
label="First Name"
variant="outlined"
margin="dense"
component='input'
/>
<ErrorMessage name="name" className="errorMsg" component="p" />
这不起作用:
<Field
name="lastName"
id="outlined-textarea"
label="Last Name"
component={TextField}
variant="outlined"
margin="dense"
/>
<ErrorMessage name="lastName" className="errorMsg" component="p" />
我创建了代码的沙箱 here.
要使用 material ui 正确挂接 formik,请使用 render prop(而不是 component
)并获取您获得的 formik field
传递给 material ui Textfield
像这样
<Field
name="lastName"
id="outlined-textarea"
label="Last Name"
render={({ field }) => <TextField {...field} />}
variant="outlined"
margin="dense"
/>
我使用 formik 和 material UI 构建了一个简单的联系页面。一切正常,除了当我使用 Material UI 组件而不是常规输入标签时。程序似乎无法读取 Material UI TextField
组件中的值。
这个有效:
<Field
name="name"
id="outlined-textarea"
label="First Name"
variant="outlined"
margin="dense"
component='input'
/>
<ErrorMessage name="name" className="errorMsg" component="p" />
这不起作用:
<Field
name="lastName"
id="outlined-textarea"
label="Last Name"
component={TextField}
variant="outlined"
margin="dense"
/>
<ErrorMessage name="lastName" className="errorMsg" component="p" />
我创建了代码的沙箱 here.
要使用 material ui 正确挂接 formik,请使用 render prop(而不是 component
)并获取您获得的 formik field
传递给 material ui Textfield
像这样
<Field
name="lastName"
id="outlined-textarea"
label="Last Name"
render={({ field }) => <TextField {...field} />}
variant="outlined"
margin="dense"
/>