Unform

Get/set field value

Unform provides an easy way to set or access input data using form reference.

1export default function MyForm() {
2 const formRef = useRef(null);
3
4 function functionThatGetsData() {
5 // Get all data
6 const allData = formRef.current.getData();
7
8 // Get single field value
9 const name = formRef.current.getFieldValue('name');
10 const street = formRef.current.getFieldValue('address.street');
11
12 console.log(allData, name, street);
13 }
14
15 function functionThatSetsData() {
16 // Set all data
17 formRef.current.setData({
18 name: 'John Doe',
19 address: {
20 street: 'Some street',
21 },
22 });
23
24 // Set single field value
25 formRef.current.setFieldValue('name', 'John Doe');
26 formRef.current.setFieldValue('address.street', 'Some street');
27 }
28
29 return (
30 <Form ref={formRef}>
31 <Input name="name" />
32
33 <Scope path="address">
34 <Input name="street" />
35 <Input name="number" />
36 </Scope>
37 </Form>
38 );
39}
Edit this page on GitHub