From a3972b7d1377afc269526d22c3a6c0cc0fc5b05d Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 14 Sep 2021 17:20:10 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Make=20`useValidatedState`=20cat?= =?UTF-8?q?ch=20errors=20and=20return=20"error"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/hooks/useValidatedState.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/frontend/src/hooks/useValidatedState.ts b/frontend/src/hooks/useValidatedState.ts index b774ef3..15785ce 100644 --- a/frontend/src/hooks/useValidatedState.ts +++ b/frontend/src/hooks/useValidatedState.ts @@ -23,7 +23,6 @@ export type ValidatedState = [T, React.Dispatch>, 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(def: T, validator: Validator): Validated const validity = React.useMemo( () => { - return validator(value) + try { + return validator(value) + } + catch (e) { + return "error" + } }, [validator, value] )