Hey Justin,
as promised :) we have a new custom hook for Field Array.
react-hook-form.com/api#useFieldArray
example below:
import React from "react"; import { useForm, useFieldArray } from "react-hook-form"; function App() { const { register, control, handleSubmit } = useForm({ // defaultValues: {}; you can populate the fields by this attribute }); const { fields, append, prepend, remove } = useFieldArray({ control, name: "test" }); return ( <form onSubmit={handleSubmit(data => console.log("data", data))}> <ul> {fields.map((item, index) => ( <li key={item.id}> <input name={`test[${index}].name`} ref={register} /> <button onClick={() => remove(index)}>Delete</button> </li> )} </ul> <section> <button type="button" onClick={() => append({ name: "test" })} > append </button> <button type="button" onClick={() => prepend({ name: "test1" })}> prepend </button> </form> ); }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hey Justin,
as promised :) we have a new custom hook for Field Array.
react-hook-form.com/api#useFieldArray
example below: