1
Fork 0
mirror of https://github.com/Steffo99/sophon.git synced 2024-12-22 23:04: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 ca065c2f13
commit 87f84b3bbf
Signed by: steffo
GPG key ID: 6965406171929D01

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. * 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 def - Default value for the state.
* @param validator - The {@link Validator} to apply. * @param validator - The {@link Validator} to apply.
*/ */
@ -34,7 +33,12 @@ export function useValidatedState<T>(def: T, validator: Validator<T>): Validated
const validity const validity
= React.useMemo( = React.useMemo(
() => { () => {
return validator(value) try {
return validator(value)
}
catch (e) {
return "error"
}
}, },
[validator, value] [validator, value]
) )