Unform
Get/set field value
Get/set field value
Unform provides an easy way to set or access input data using the form ref.
1export default function MyForm() {2 const formRef = useRef(null);34 function functionThatGetsData() {5 // Get all data6 const allData = formRef.current.getData();78 // Get single field value9 const name = formRef.current.getFieldValue('name');10 const street = formRef.current.getFieldValue('address.street');1112 console.log(allData, name, street);13 }1415 function functionThatSetsData() {16 // Set all data17 formRef.current.setData({18 name: 'John Doe',19 address: {20 street: 'Some street',21 },22 });2324 // Set single field value25 formRef.current.setFieldValue('name', 'John Doe');26 formRef.current.setFieldValue('address.street', 'Some street');27 }2829 return (30 <Form ref={formRef}>31 <Input name="name" />3233 <Scope path="address">34 <Input name="street" />35 <Input name="number" />36 </Scope>37 </Form>38 );39}