reactjs - Formik 和 arrayField 与 Material ui 集成

标签 reactjs formik yup

enter image description here

我正在尝试构建一个 formik/material ui 框架 - 但数组字段结构似乎无法正常工作。

https://codesandbox.io/s/jovial-albattani-ztf10

当您尝试添加第三个 friend 并开始输入时 - 我收到错误“试图控制不受控制”的问题?

Warning: A component is changing a controlled input of type text to be uncontrolled. Input elements should not switch from controlled to uncontrolled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component. 

--在这些文档中我没有看到这个错误? https://codesandbox.io/s/github/formik/formik/tree/master/examples/field-arrays?from-embed=&file=/index.js https://formik.org/docs/examples/field-arrays

------------- -- 这是使用 useformik 并且不适用于 arrayfields

https://codesandbox.io/s/boring-kare-ixonw

当我点击“添加成员”时,出现错误“无法读取未定义的属性‘setFormikState’”

我已尝试遵循这些示例 - 但我不确定是什么导致了此错误。 https://formik.org/docs/api/fieldarray

我尝试过使用 fieldArrayHelper - 但似乎不需要

https://codesandbox.io/s/boring-kare-ixonw?file=/src/_Globals/GeneralFormik/FieldHandler.js

最佳答案

当输入接收到未定义值时,React 将其解释为意味着输入不受控制。你可以在这里看到https://github.com/formium/formik/issues/28 .

因此,您需要的是在单击添加 friend 时初始化一个 friend 值,如下所示(FieldArrayHandler.js)

<Button
   disabled={parent.disabled}
   variant="contained"
   color="primary"
   type="button"
   onClick={() =>
     arrayHelpers.push({ firstName: "", lastName: "" })
   }
>
   Add a friend
</Button>

您可以看到一个正在运行的演示 here

关于reactjs - Formik 和 arrayField 与 Material ui 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67738528/

相关文章:

reactjs - 在 typescript 中创建自己的 react 路线类

javascript - 如何在 axios 中设置 POST - multipart/form-data 的 MIME 类型?

react-native - React Native,Formik-Yup 验证错误未显示

javascript - 如何使用 Yup 检查字符串中的特殊字符?

reactjs - Redux 状态通过数据库持久化

javascript - react native : Child ListView will not update after element in parent is changed

reactjs - 使用 YUP 验证文件存在

reactjs - 默认值不适用于 react-select 和 Formik

javascript - 是的动态验证链

reactjs - 使用 Formik 进行 React-select 不更新选择字段而是执行其他所有操作