useField()
React hook that subscribes to a field store and returns the current value, validation state, and change handler.
The component re-renders whenever the field's input or state changes.
ts
import { useField } from 'formtery';ts
function useField<Input, Value>(
store: FieldStore<Input, Value>,
ref?: React.Ref<any>,
): UseFieldResult<Input, Value>;Params
storeThe field store to subscribe torefExternal ref (Optional)
Returns
ts
interface UseFieldResult<Input, Value> {
ref: React.Ref<any>;
value: Input;
state: ValidationResult<Value>;
setValue: (value: React.SetStateAction<Input>) => void;
}refAttach to the element to enable auto-focus on validation error.valueCurrent input value. Use as the controlled value of your input element.setValueCall with a new value whenever the input changes.stateCurrent validation result.
Notes
Ref Forwarding
If you need to attach your own ref to the element alongside formtery's internal ref, pass it as the second argument. The refs are merged automatically.
tsx
function TextInput({ inputRef, store }: TextInputProps) {
const { ref, value, setValue, state } = useField(store, inputRef);
return (
<input
ref={ref}
value={value}
onChange={(e) => setValue(e.target.value)}
/>
);
}