From 1e6845b422ab43f1b63453d4086699ce75f96c03 Mon Sep 17 00:00:00 2001 From: Muhun Kim Date: Sat, 8 Aug 2020 20:15:03 +0900 Subject: [PATCH] Checking form result in useEffect callback FIXED: I moved to the useEffect callback, it was work well. --- src/app/app.tsx | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/app/app.tsx b/src/app/app.tsx index 9aae338..94681a3 100644 --- a/src/app/app.tsx +++ b/src/app/app.tsx @@ -38,9 +38,23 @@ export default function App({ optOutUrl }: OptOutUrl) { const handleSubmit = async (e: SubmitEvent) => { e.preventDefault() dispatch(submitCreator(e.target)) - if (result === Result.invalid || result === Result.blocked) return - if (isKeepLogin) saveAccountInfo(getElementValues(e.target)) + } + + useEffect(() => { + setKeepLogin(isKeepLogin) + submitWhenKeepLogin() + }, [isKeepLogin]) + + useEffect(() => { + if ( + result === Result.idle || + result === Result.invalid || + result === Result.blocked + ) { + // prettier-ignore + return + } if (result === Result.correct) { location.href = 'main.do' @@ -52,13 +66,10 @@ export default function App({ optOutUrl }: OptOutUrl) { return } - throw Error('Unexpect state flow in handleSubmit') - } - - useEffect(() => { - setKeepLogin(isKeepLogin) - submitWhenKeepLogin() - }, [isKeepLogin]) + throw Error( + `Unexpect result flow in handleSubmit' current value: ${Result[result]}` + ) + }, [result]) const onChange = () => { if (isWrong) dispatch(IDLE)