1
Fork 0
mirror of https://github.com/Steffo99/sophon.git synced 2024-12-22 06:44:21 +00:00

🔧 Make useValidatedState catch errors and return "error"

This commit is contained in:
Steffo 2021-09-14 17:20:10 +02:00
parent 84c69e96e2
commit a3972b7d13

View file

@ -23,7 +23,6 @@ export type ValidatedState<T> = [T, React.Dispatch<React.SetStateAction<T>>, Val
/**
* Hook that extends {@link React.useState} by applying a {@link Validator} to the stored value, returning its results to the caller.
*
* @todo Improve this docstring.
* @param def - Default value for the state.
* @param validator - The {@link Validator} to apply.
*/
@ -34,7 +33,12 @@ export function useValidatedState<T>(def: T, validator: Validator<T>): Validated
const validity
= React.useMemo(
() => {
return validator(value)
try {
return validator(value)
}
catch (e) {
return "error"
}
},
[validator, value]
)