微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Formik 和 arrayField 与材料 ui 的集成

如何解决Formik 和 arrayField 与材料 ui 的集成

enter image description here

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

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

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

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 并且不适用于数组字段

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

解决方法

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

所以你需要的是在点击 Add a Friend 时初始化一个朋友值 (FieldArrayHandler.js)

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

你可以看到一个演示here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?