question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Update values in useFieldArray from outside form

See original GitHub issue

Hi,

I’m not sure if I’m doing something wrong, or that I’ve found a bug. Within my project I’m using a useFieldArray which contains categories. I can insert a category from a modal. This works fine. But when I try to update the value of this category nothing happens when I use setValue, or the fields are removed when I reset the values baesd on getValues() because the categories aren’t an array yet, but save as categories[0].title: "test".

You can find my simplified use case over here: https://codesandbox.io/s/old-glitter-wffzq Only works for one value now because I’d hardcode the index in handleUpdate

How to update a value correctly from outside the form? Thanks in advance for your help!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
rnnyrkcommented, Mar 20, 2020

@bluebill1049 No worries about being a but later. We’re all human haha 😃

Taken from your example; Screenshot 2020-03-20 at 11 59 23

You can see the input value being updated, but the logs and the “test” behind the the input still showing the old value.

My use case is that I add a category of useFieldArray from within a modal. Then the category title isn’t shown in an input, but as title of a block.

Screenshot 2020-03-20 at 12 03 23

So the red circle is the title from the useFieldArray, but to actually save the value (and the order) within the useFieldArray I use a hidden input. I use hidden inputs because I have to send the values to my backend

So my code looks like:

      {fields.map((field, index) => {
        return (
          <EditionsCategory key={`edition_category_${index}`}>
            <Input
              name={`categories[${index}].title`}
              type="hidden"
              defaultValue={field?.title}
              register={register()}
              noFieldset
            />
            <Input
              name={`categories[${index}].order`}
              type="hidden"
              defaultValue={index}
              register={register()}
              noFieldset
            />

            <EditionsCategoryHeader
              title={field?.title}
              handleDelete={() => handleDeleteCategory(index)}
              handleEdit={() => handleEditCategory(field.title, index)}
              handleMove={(direction) => handleMoveCategory(direction, index)}
            />
            <EditableTable
              columns={memoHeaderCustomCategory}
              data={[]}
              newRowForm={AddCustomCategoryForm}
            />
          </EditionsCategory>
        );
      })}

The green circle opens a modal to edit the title value which is prefilled within the modal. When I save the edit modal the handleUpdate from the codesandbox is triggered. But as seen in both codesandbox examples the field.title isn’t updated correctly.

Hope you understand my use case and issue now 😃

1reaction
bluebill1049commented, Mar 18, 2020

thank you @rnnyrk it’s just a bit easier for me to take a look tonight.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useFieldArray - Simple React forms validation
Update input/inputs at a particular position, updated fields will get unmount and remount. If this is not desired behavior, please use setValue API...
Read more >
Updating default values of a React-Hook-Form, does not ...
The problem is that when the state updates, form default values are updated, but the method useFieldArray attribute fields are not updated ...
Read more >
Effective forms: building dynamic array fields with useFieldArray
update – updates input/inputs at a particular position; replace – replaces the total field array values; remove – removes input/inputs at a particular...
Read more >
React Hook Form - useFieldArray - YouTube
In this session, we are taking a look at the useFieldArray custom hook to manage your dynamic form fields.working example: ...
Read more >
React Hook Form - useFieldArray - Get Current Values
React Hook Form - useFieldArray - Get Current Values. 0. Embed Fork Create Sandbox Sign in. Sandbox Info. React Hook Form - useFieldArray...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found