From f923360d1b37c31f75375ad2788b69ba279ac668 Mon Sep 17 00:00:00 2001 From: wenyan Date: Tue, 22 Apr 2025 16:21:09 -0400 Subject: [PATCH] refactor(useUnmount): move ref assignment from render to layout phase --- packages/usehooks-ts/src/useUnmount/useUnmount.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/usehooks-ts/src/useUnmount/useUnmount.ts b/packages/usehooks-ts/src/useUnmount/useUnmount.ts index 761bca97..022d1e28 100644 --- a/packages/usehooks-ts/src/useUnmount/useUnmount.ts +++ b/packages/usehooks-ts/src/useUnmount/useUnmount.ts @@ -1,4 +1,5 @@ import { useEffect, useRef } from 'react' +import { useIsomorphicLayoutEffect } from 'src/useIsomorphicLayoutEffect' /** * Custom hook that runs a cleanup function when the component is unmounted. @@ -15,7 +16,9 @@ import { useEffect, useRef } from 'react' export function useUnmount(func: () => void) { const funcRef = useRef(func) - funcRef.current = func + useIsomorphicLayoutEffect(()=>{ + funcRef.current = func + },[func]) useEffect( () => () => {