React Hook Form with Dynamic Fields Not Updating State Correctly on Input Change
I've been researching this but I'm working with React and React Hook Form (v7) to create a dynamic form that allows users to add multiple sets of input fields... However, I'm encountering an issue where the state of the form fields does not update correctly when I change the inputs. I have a function to add new fields dynamically, but the onChange event for these fields seems to be misbehaving. Here’s a simplified version of my code: ```javascript import React from 'react'; import { useForm, useFieldArray } from 'react-hook-form'; const DynamicForm = () => { const { control, handleSubmit } = useForm({ defaultValues: { users: [{ name: '', age: '' }], }, }); const { fields, append, remove } = useFieldArray({ control, name: 'users' }); const onSubmit = data => { console.log(data); }; return ( <form onSubmit={handleSubmit(onSubmit)}> {fields.map((field, index) => ( <div key={field.id}> <input {...register(`users.${index}.name`)} placeholder="Name" /> <input {...register(`users.${index}.age`)} placeholder="Age" type="number" /> <button type="button" onClick={() => remove(index)}>Remove</button> </div> ))} <button type="button" onClick={() => append({ name: '', age: '' })}>Add User</button> <button type="submit">Submit</button> </form> ); }; export default DynamicForm; ``` When I try to modify the fields, the updates are not reflected in the form’s state. For example, if I add users and then change an input for one of them, the console log in `onSubmit` shows the previous values instead of the updated ones. I’ve already checked that the `register` function is being called correctly, and I attempted to add a `setValue` function call, but it didn’t resolve the issue. I’m also using React 17 and ensuring there are no conflicting libraries that might cause issues. Has anyone else run into this problem or have suggestions on how to ensure the state updates correctly when using dynamic fields with React Hook Form? For context: I'm using Javascript on Debian.