javascript - React useForm Hook with array of elemens - Stack Overflow

I have to handle a form and I'm using the useForm library of "react-hook-form", the prob

I have to handle a form and I'm using the useForm library of "react-hook-form", the problem is that I need to handle multiple data, such as arrays and objects, for now I've only managed to handle one array, but I can't handle two for I can't duplicate this part of the code:


const {
    register,
    handleSubmit,
    formState: { errors },
    control,
  } = useForm();

 const { fields, append, remove } = useFieldArray({
    control,
    name: "habilidades",
  });

I have this two fields:

<div>
  <label>Habilidades</label>
  {fields.map((item, index) => {
    return (
      <div key={item.id}>
        <input
          {...register(`habilidades.${index}`, { required: true })}
        />

        <button type="button" onClick={() => remove(index)}>
          Eliminar
        </button>
      </div>
    );
  })}
  <button
    type="button"
    onClick={() => {
      append("");
    }}>
    Nueva habilidad
  </button>
</div>

<div>
  <label>Aptitudes</label>

  {fields.map((item, index) => {
    return (
      <div key={item.id}>
        <input {...register(`aptitudes.${index}`, { required: true })} />

        <button type="button" onClick={() => remove(index)}>
          Eliminar
        </button>
      </div>
    );
  })}

  <button
    type="button"
    onClick={() => {
      append("");
    }}>
    Nueva habilidad
  </button>
</div>

How can I manage each of the fields independently? The problem is that it only recognizes the "habilidades" field and I need to also handle the "aptitudes" field without the two ing together.

 const { fields, append, remove } = useFieldArray({
    control,
    name: "habilidades",
    name: "aptitudes"
  });

I have tried to put two names but it does not work.

What I expect is that the form returns something like this:

{
   habilidades: ["example1", "example2", "example3"]
   aptitudes: ["example4", "example5", "example6"],
}

I have to handle a form and I'm using the useForm library of "react-hook-form", the problem is that I need to handle multiple data, such as arrays and objects, for now I've only managed to handle one array, but I can't handle two for I can't duplicate this part of the code:


const {
    register,
    handleSubmit,
    formState: { errors },
    control,
  } = useForm();

 const { fields, append, remove } = useFieldArray({
    control,
    name: "habilidades",
  });

I have this two fields:

<div>
  <label>Habilidades</label>
  {fields.map((item, index) => {
    return (
      <div key={item.id}>
        <input
          {...register(`habilidades.${index}`, { required: true })}
        />

        <button type="button" onClick={() => remove(index)}>
          Eliminar
        </button>
      </div>
    );
  })}
  <button
    type="button"
    onClick={() => {
      append("");
    }}>
    Nueva habilidad
  </button>
</div>

<div>
  <label>Aptitudes</label>

  {fields.map((item, index) => {
    return (
      <div key={item.id}>
        <input {...register(`aptitudes.${index}`, { required: true })} />

        <button type="button" onClick={() => remove(index)}>
          Eliminar
        </button>
      </div>
    );
  })}

  <button
    type="button"
    onClick={() => {
      append("");
    }}>
    Nueva habilidad
  </button>
</div>

How can I manage each of the fields independently? The problem is that it only recognizes the "habilidades" field and I need to also handle the "aptitudes" field without the two ing together.

 const { fields, append, remove } = useFieldArray({
    control,
    name: "habilidades",
    name: "aptitudes"
  });

I have tried to put two names but it does not work.

What I expect is that the form returns something like this:

{
   habilidades: ["example1", "example2", "example3"]
   aptitudes: ["example4", "example5", "example6"],
}
Share Improve this question edited Dec 8, 2022 at 2:53 Romera02 asked Dec 8, 2022 at 2:52 Romera02Romera02 751 gold badge2 silver badges7 bronze badges 1
  • 1 how about use like register("name.firstName.0") {name: { firstName: [ 'value' ] }} react-hook-form./api/useform/register – MR.Jeon Commented Dec 8, 2022 at 4:53
Add a ment  | 

1 Answer 1

Reset to default 3

Pretty sure you need to add a useFieldArray for each array field:

const { fields, append, remove } = useFieldArray({
    control,
    name: "habilidades",
  });

const { fields: aptitudesFields, append: appendAptitudes, remove: removeAptitudes } = useFieldArray({
    control,
    name: "aptitudes"
  });

Or, rather than renaming the aptitudes methods, you could use form context to split each array field into its own ponent where "fields", "append", and "remove" would be locally scoped.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744318115a4568282.html

相关推荐

  • javascript - React useForm Hook with array of elemens - Stack Overflow

    I have to handle a form and I'm using the useForm library of "react-hook-form", the prob

    8天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信