use-validated-state

Validate state and get last valid value
Import

Usage

Hook validates state with a given rule each time state is set. It returns an object with current validation state, last valid value and current value:

const [{ lastValidValue, value, valid }, setValue] = useValidatedState(
'valid',
(value) => value === 'valid'
);
lastValidValue; // -> valid
value; // -> valid
valid; // -> true
setValue('invalid');
lastValidValue; // -> valid
value; // -> invalid
valid; // -> false

Definition

function useValidatedState<T>(
initialValue: T,
validation: (value: T) => boolean
): readonly [
{
value: T;
lastValidValue: T;
valid: boolean;
},
(val: T) => void
];
Build fully functional accessible web applications faster than ever
Feedback
Your feedback is most valuable contribution to the project, please share how you use Mantine, what features are missing and what is done good
Leave feedback