Replies: 1 comment
|
import { useForm, useFieldArray } from 'react-hook-form';
function Example() {
const { register, control, handleSubmit } = useForm({
defaultValues: { items: [{ value: 'a' }, { value: 'b' }] },
});
const { fields, append, remove } = useFieldArray({
control,
name: 'items',
});
return (
<form onSubmit={handleSubmit(({ items }) => {
const data = items.map(({ value }) => value);
console.log(data); // ['a', 'b'] initially
})}>
{fields.map((field, index) => (
<div key={field.id}>
<input {...register(`items.${index}.value`)} />
<button type="button" onClick={() => remove(index)}>Remove</button>
</div>
))}
<button type="button" onClick={() => append({ value: '' })}>Add</button>
<button type="submit">Submit</button>
</form>
);
}Map the submitted |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I want to get array of string from react hook form like this
const data = ['a','b'] using fieldArray
All reactions