Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/@react-aria/utils/src/useEffectEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import {useLayoutEffect} from './useLayoutEffect';
// before all layout effects, but is available only in React 18 and later.
const useEarlyEffect = React['useInsertionEffect'] ?? useLayoutEffect;

export function useEffectEvent<T extends Function>(fn?: T): T {
// Starting with React 19.2, this hook has been internalized.
const useModernEffectEvent = React['useEffectEvent'] ?? useLegacyEffectEvent;

function useLegacyEffectEvent<T extends Function>(fn?: T): T {
const ref = useRef<T | null | undefined>(null);
useEarlyEffect(() => {
ref.current = fn;
Expand All @@ -28,3 +31,8 @@ export function useEffectEvent<T extends Function>(fn?: T): T {
return f?.(...args);
}, []);
}

export function useEffectEvent<T extends Function>(fn?: T): T {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really sure why fn was optional.... this is an internal hook for us (not documented), so I think we should probably have it be required, then we can get rid of this extra noop function

let noop = useCallback(() => {}, []);
return useModernEffectEvent(fn ?? noop);
}