From 4a901ca9a1010ace0c93e28814496ffb8a1bbe00 Mon Sep 17 00:00:00 2001 From: UNIkeEN <94227543+UNIkeEN@users.noreply.github.com> Date: Thu, 28 Nov 2024 19:28:40 +0800 Subject: [PATCH] refactor(frontend): refactor 404 page route logic --- frontend/src/pages/404.tsx | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/frontend/src/pages/404.tsx b/frontend/src/pages/404.tsx index 4d65cd3..4e2e034 100644 --- a/frontend/src/pages/404.tsx +++ b/frontend/src/pages/404.tsx @@ -11,18 +11,18 @@ const NotFoundPage = () => { const [seconds, setSeconds] = useState(3); useEffect(() => { - if (seconds > 1) { - const timer = setTimeout(() => { - setSeconds(seconds - 1); - }, 1000); - return () => clearTimeout(timer); - } else { - const timer = setTimeout(() => { - router.push('/home'); - }, 1500); - return () => clearTimeout(timer); - } - }, [seconds, router]); + const interval = setInterval(() => { + setSeconds((prev) => { + if (prev === 1) { + clearInterval(interval); + router.push('/home'); + } + return prev - 1; + }); + }, 1000); + + return () => clearInterval(interval); + }, [router]); return ( <> @@ -39,14 +39,12 @@ const NotFoundPage = () => { }} > - - - {t('NotFoundPage.text', { seconds: seconds })} - + + {t('NotFoundPage.text', { seconds })} ); }; -export default NotFoundPage; +export default NotFoundPage; \ No newline at end of file