简体   繁体   English

使用React-hook-form的useFieldArray点击append怎么会出现空字段?

[英]How can I get an empty field when I click on the append using the useFieldArray of React-hook-form?

Whenever I click on the append button, the generated field value printed as a [object object] but I want to get a blank field.每当我点击 append 按钮时,生成的字段值打印为[object object]但我想得到一个空白字段。 I want to output like this: "fieldName": ["value1","value2"...n] .我想要这样的 output: "fieldName": ["value1","value2"...n] In this code, I get the output as I like, but every time I have to remove the [object obejct] that is printed in the appended field.在此代码中,我得到了 output,但每次我都必须删除附加字段中打印的[object obejct] 示例图像:

code link: https://codesandbox.io/s/practical-star-jif19c?file=/src/index.js:162-177代码链接: https://codesandbox.io/s/practical-star-jif19c?file=/src/index.js:162-177

Since your array contains only origin names and not objects of origin you should put "" inside append function not {} object.由于您的数组仅包含原始名称而不包含原始对象,因此您应该将""放入append function 而不是{} object。

<Button
    variant="contained"
    component="label"
    type="button"
    onClick={() => append('')}
>
    Add Items
</Button>

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

相关问题 为什么在 react-hook-form 中对动态表单使用 useFieldArray() 时不能关注当前字段? - Why can't I focus on the current field when using useFieldArray() for dynamic form in react-hook-form? 如何使用 useFieldArray 钩子验证 React Hook Form 中的数组字段? - How can I validate array field in React Hook Form with the useFieldArray hook? React Native 中的 React-Hook-Form useFieldArray - React-Hook-Form useFieldArray in React Native 维护 useFieldArray react-hook-form 的状态 - Maintain state of useFieldArray react-hook-form react-hook-form id 未使用 useFieldArray 保存 - react-hook-form id not saved with useFieldArray 如何使用带有 useFieldArray 的 react-hook-form 在动态表单上使用 yup 验证 - How to use yup validation on dynamic form using react-hook-form with useFieldArray 当我折叠、添加或删除面板时,React-hook-form 字段值会丢失 - React-hook-form field value get lost when i collapse, add or delete panel 如何将 yup validationSchema 与 react-hook-form 一起使用? - How can I use yup validationSchema with react-hook-form? 带有 useFieldArray 的 React-Hook Form:TextField 值未显示在控制台中。 我该如何解决? - React-Hook Form with useFieldArray: TextField value does not show up in the console. How can I fix it? 如何使用 react-hook-form 验证我的密码 - How Can I validate my password using react-hook-form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM