简体   繁体   English

Formik 的 forms 不识别材质 UI 组件的文本字段值?

[英]Formik's forms don't recognize material UI component's text field value?

I built a simple contact page using formik and material UI.我使用 formik 和 Material UI 构建了一个简单的联系页面。 Everything works except for when I use the Material UI components instead of regular input tags.除了我使用 Material UI 组件而不是常规输入标签时,一切都正常。 It seems like the program can't read the value inside the Material UI TextField component.似乎程序无法读取 Material UI TextField组件中的值。

This works:这有效:

                     <Field 
                        name="name" 
                        id="outlined-textarea"
                            label="First Name"
                            variant="outlined"
                            margin="dense"
                            component='input'
                            />
                    <ErrorMessage name="name" className="errorMsg" component="p" />

This doesn't work:这不起作用:

                      <Field
                        name="lastName"
                        id="outlined-textarea"
                        label="Last Name"
                        component={TextField}
                        variant="outlined"
                        margin="dense"
                    />
         <ErrorMessage name="lastName" className="errorMsg" component="p" />

I created a sandbox of the codehere.我在这里创建了一个代码沙箱。

To hook formik properly with material ui, use render prop (instead of component ) and grab the formik field which you get and pass it down to material ui Textfield要将 formik 与材质 ui 正确挂钩,请使用render prop(而不是component )并获取您获得的 formik field并将其传递给材质 ui Textfield

Like this像这样

<Field
    name="lastName"
    id="outlined-textarea"
    label="Last Name"
    render={({ field }) => <TextField {...field} />}
    variant="outlined"
    margin="dense"
  />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM