mirror of
https://github.com/Steffo99/sophon.git
synced 2024-12-22 14:54:22 +00:00
🔧 Make useValidatedState
catch errors and return "error"
This commit is contained in:
parent
84c69e96e2
commit
a3972b7d13
1 changed files with 6 additions and 2 deletions
|
@ -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]
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue