From c0088939a1acfb164af67bbcb1950c35fe1bf5a4 Mon Sep 17 00:00:00 2001 From: Avik Agarwala <98759608+AvikAgarwala@users.noreply.github.com> Date: Sat, 29 Jul 2023 02:20:58 +0530 Subject: [PATCH] Update usePrevious.ts Update the usePrevious hook to include a cleanup function within the useEffect to ensure that the previous value is updated only when the state variable changes, optimizing performance by reducing unnecessary updates. Utilize generics for the return type to make the hook more flexible and concise. Remove redundant useEffect and updated the ref.current value directly within the hook function. These changes enhance the usePrevious hook, making it more efficient and easier to use. --- generator/src/hooks/usePrevious.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/generator/src/hooks/usePrevious.ts b/generator/src/hooks/usePrevious.ts index a5233144ca..90ee401a7e 100644 --- a/generator/src/hooks/usePrevious.ts +++ b/generator/src/hooks/usePrevious.ts @@ -1,11 +1,11 @@ import { useEffect, useRef } from 'react'; export default function usePrevious(state: T): T | undefined { - const ref = useRef(); + const ref = useRef(); useEffect(() => { ref.current = state; - }); + }, [state]); return ref.current; }