diff --git a/packages/react-hooks/src/useIsMountedRef.ts b/packages/react-hooks/src/useIsMountedRef.ts index b135c0f..d9b32b1 100644 --- a/packages/react-hooks/src/useIsMountedRef.ts +++ b/packages/react-hooks/src/useIsMountedRef.ts @@ -3,7 +3,7 @@ import type React from 'react'; -import { useEffect, useRef } from 'react'; +import { useLayoutEffect, useRef } from 'react'; import { createNamedHook } from './createNamedHook.js'; @@ -12,7 +12,10 @@ export type MountedRef = React.MutableRefObject; function useIsMountedRefImpl (): MountedRef { const isMounted = useRef(false); - useEffect((): () => void => { + // A layout effect runs while the component is committed, before the browser + // can deliver an event to it. A passive effect runs later, so a click that + // lands in between found the ref still false and was dropped. + useLayoutEffect((): () => void => { isMounted.current = true; return (): void => {