Unform

useField

useField

useField is the main hook from Unform. To use it, you need to provide a name, passed as a parameter, when declaring it on your functional component.

import { useField } from '@unform/core';
const { ... } = useField(name);

Properties

The following properties are returned by the useField hook:

const { ... } = useField(name);
PropDescription
fieldName: stringA unique fieldname
registerField: (field: UnformField): voidThe method used to register a field on Unform. See below for more information
defaultValue: stringThe default value of the field (will be populated based on initialData)
clearError: () => voidCleans the error message
error: stringReturns the error message

registerField

When registering a field, you can pass some properties to the registerField method.

PropDescriptionRequired
name: stringThe field identifier. Always provide it with the fieldName returned from useFieldYes
refField ref. You can use the useRef hook from ReactYes
setValue: (ref: any, value: T) => voidThe logic to set the field value. This will be saved on Unform state.No
clearValue: (ref: any, value: T) => voidCleans the field valueNo
getValue: (ref: any, newValue: T) => voidCan be used to transform a value before it is savedNo
path: string => voidPath to getting the field value. If you don't provide a getValue it's requiredNo
Edit this page on GitHub