简体   繁体   English

带有 useFieldArray 的 React-Hook Form:TextField 值未显示在控制台中。 我该如何解决?

[英]React-Hook Form with useFieldArray: TextField value does not show up in the console. How can I fix it?

I wanted to enter the word Product but when I submit it, it does not show up in the console.我想输入Product这个词,但是当我提交它时,它没有显示在控制台中。

在此处输入图像描述

What shows up in the console:控制台中显示的内容:

As you can see here, the word Product does not appear in the console.正如您在这里看到的, Product一词没有出现在控制台中。 Any idea on how I can solve this?关于如何解决这个问题的任何想法?

在此处输入图像描述

This is the codesandbox link: https://codesandbox.io/s/react-hook-form-usefieldarray-nested-arrays-forked-vjwbp?file=/src/index.js这是 codesandbox 链接: https://codesandbox.io/s/react-hook-form-usefieldarray-nested-arrays-forked-vjwbp?file=/src/index.js

this is the fieldArray.js where the input fields for products is这是产品输入字段所在的 fieldArray.js

import React from "react";
import { useFieldArray } from "react-hook-form";
import NestedArray from "./nestedFieldArray";
import { TextField } from "@mui/material";

let renderCount = 0;

export default function Fields({ control, register, setValue, getValues }) {
  const { fields, append, remove, prepends } = useFieldArray({
    control,
    name: "test"
  });

  renderCount++;

  return (
    <>
      <ul>
        {fields.map((item, index) => {
          return (
            <li key={item.id}>
              {/* <select
                ref={register()}
                name={`test[${index}].name`}
                defaultValue={item.name}
              >
                <option value="">Select</option>
                <option value="10">ItemA</option>
                <option value="20">ItemB</option>
              </select> */}
              {/* {index + 1}  to show the qty */}
              <TextField
                name={`test[${index}].name`}
                refer={register()}
                defaultValue={item.name}
              />

              <button type="button" onClick={() => remove(index)}>
                Delete
              </button>
              <NestedArray nestIndex={index} {...{ control, register }} />
            </li>
          );
        })}
      </ul>

      <section>
        <button
          type="button"
          onClick={() => {
            append({ name: "append" });
          }}
        >
          Add product
        </button>
      </section>

      <span className="counter">Render Count: {renderCount}</span>
    </>
  );
}

you can change it try way:你可以改变它尝试方式:

fieldArray.js字段数组.js

<TextField
  name={`test[${index}].nestedArray[${index}].product`}
  inputRef={register({ required: true })}
  defaultValue={item.name}
/>;

在此处输入图像描述

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

相关问题 React-Hook Form:如何将数量的值转换为 useFieldArray 内的数字? - React-Hook Form: How do I convert the value of the quantity into a number that is inside the useFieldArray? 如何使用 useFieldArray 钩子验证 React Hook Form 中的数组字段? - How can I validate array field in React Hook Form with the useFieldArray hook? 使用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? 为什么在 react-hook-form 中对动态表单使用 useFieldArray() 时不能关注当前字段? - Why can't I focus on the current field when using useFieldArray() for dynamic form in react-hook-form? 使用 React-Hook 表单,如何在 React Native 中将数据发布到我的 MongoDB - Using React-Hook Form, How do I post data to my MongoDB in React Native 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 React-hook setState 无法按预期工作 - React-hook setState does not work as expected 如何使用react-hook按时更新state - How to update state on time with react-hook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM