@piplup/rhf-coreHooks
useFieldState
Reads validation and interaction state for a single field path.
useFieldState is a low-level hook for reading the state of one field from the current form. It is helpful when you want to react to a field being invalid, touched, dirty, disabled, or currently validating, but you do not need the adapter props returned by useFieldStateAdapter or useControllerAdapter.
The hook returns field-level state plus a few useful form-derived flags such as disabled and isSubmitting.
Import
import { useFieldState } from '@piplup/rhf-core';Usage
'use client';import { HtmlButtonElement } from '@piplup/rhf-adapters/html';import { useFieldState } from '@piplup/rhf-core';import { useForm } from 'react-hook-form';import type { Control } from 'react-hook-form';type FieldValues = { password: string;};function PasswordStatus({ control }: { control?: Control<FieldValues> }) { const fieldState = useFieldState({ control, name: 'password', }); if (!fieldState.isTouched) { return <p>Start typing to validate your password.</p>; } return ( <p style={{ color: fieldState.invalid ? 'red' : 'green' }}> {fieldState.invalid ? (fieldState.error?.message ?? 'Password is invalid.') : 'Password looks good.'} </p> );}export default function Page() { const { control, handleSubmit, register } = useForm({ defaultValues: { password: '', }, }); return ( <form onSubmit={handleSubmit((values) => alert(JSON.stringify(values, null, 2)))} noValidate> <div> <label htmlFor="password">Password</label> <input id="password" type="password" {...register('password', { minLength: { message: 'Password must be at least 8 characters.', value: 8, }, required: 'Password is required.', })} className="border rounded block px-2" /> </div> <PasswordStatus control={control} /> <div className="flex flex-row gap-2"> <HtmlButtonElement className="bg-black hover:bg-black/90 px-4 py-1 text-white rounded" control={control} type="submit" > Submit </HtmlButtonElement> <HtmlButtonElement className="border border-black hover:border-black/90 px-4 py-1 rounded" control={control} type="reset" > Reset </HtmlButtonElement> </div> </form> );}Props
Prop
Type
Return value
Prop
Type
Notes
- This hook is best when you want state only and plan to render your own UI from it.
- It reads a single field path and does not provide input binding helpers such as
valueoronChange. - If you want helper text, class names, or style to be composed for you, use
useFieldStateAdapterinstead.