MCPcopy Create free account
hub / github.com/react-hook-form/react-hook-form / App

Function App

examples/V6/FieldArray.tsx:9–68  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

7}
8
9export default function App() {
10 const { register, handleSubmit, errors } = useForm();
11 const [size, setSize] = useState(1);
12 const onSubmit = (data) => {
13 alert(JSON.stringify(data));
14 };
15
16 console.log(errors);
17
18 return (
19 <div className="App">
20 <form onSubmit={handleSubmit(onSubmit)}>
21 {createArrayWithNumbers(size).map((number) => {
22 return (
23 <div key={number}>
24 <div>
25 <label htmlFor="firstName">First Name</label>
26 <input
27 name={`firstName[${number}]`}
28 placeholder="first name"
29 ref={register({ required: true })}
30 />
31 </div>
32
33 <div>
34 <label htmlFor="lastName">Last Name</label>
35 <input
36 name={`lastName[${number}]`}
37 placeholder="last name"
38 ref={register({ required: true })}
39 />
40 </div>
41
42 <div>
43 <label htmlFor="email">Email</label>
44 <input
45 name={`email[${number}]`}
46 placeholder="email"
47 ref={register({ required: true })}
48 />
49 </div>
50
51 <hr />
52 </div>
53 );
54 })}
55
56 <button type="button" onClick={() => setSize(size + 1)}>
57 Add Person
58 </button>
59 <br />
60 <div style={{ color: 'red' }}>
61 {Object.keys(errors).length > 0 &&
62 'There are errors, check your console.'}
63 </div>
64 <button type="submit">Submit</button>
65 </form>
66 </div>

Callers

nothing calls this directly

Calls 4

useFormFunction · 0.85
handleSubmitFunction · 0.85
registerFunction · 0.85
createArrayWithNumbersFunction · 0.70

Tested by

no test coverage detected