From d09fb240d77e1ee26652f7e84ce2bc3b486a1402 Mon Sep 17 00:00:00 2001 From: chienweiluo Date: Fri, 15 Sep 2023 03:30:26 +0800 Subject: [PATCH] Apply AbortController for the unmount in useEffectState.tsx --- src/hooks/useEffectState.tsx | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/hooks/useEffectState.tsx b/src/hooks/useEffectState.tsx index 02833f2..7823cc0 100644 --- a/src/hooks/useEffectState.tsx +++ b/src/hooks/useEffectState.tsx @@ -47,12 +47,22 @@ export default function useEffectState( // Value const [stateValue, setStateValue] = React.useState(defaultValue); + const abortController = new AbortController(); + // Set State const setEffectVal = useEvent((nextValue: Updater) => { notifyEffectUpdate(() => { - setStateValue(nextValue); - }); + if (!abortController.signal.aborted) { + setStateValue(nextValue); + } + }, abortController.signal); }); + useEffect(() => { + return () => { + abortController.abort(); + }; + }, []); + return [stateValue, setEffectVal]; }