Skip to content

useEffectEvent

useEffectEvent hook polyfilled for React versions below 19.

Returns a stable event-handler wrapper whose identity never changes between renders, yet always delegates to the latest version of fn captured at render time. This makes it safe to call inside effects without listing it as a dependency — the handler can freely read current props, state, or context without causing the effect to re-run.

Implemented on top of {@link useMemoizedFn}, which provides the same identity-stable / always-fresh semantics using useLayoutEffect and a ref.

Types

UseEffectEventProps

  • @template T - The type of the event handler function. Must be callable with any arguments and return any value.

Parameters accepted by useEffectEvent.

PropertyTypeRequiredDescription
fnTThe event handler function to wrap. The returned stable wrapper always delegates to the latest version of fn captured at render time, so it is safe to read props, state, or context without being listed as an effect dependency.

UseEffectEventResult

  • @template T - The type of the original handler function.

Return value of useEffectEvent.

A stable function with the same signature as fn. Its identity never changes between renders, making it safe to pass as a non-reactive dependency to effects. Internally it always delegates to the latest version of fn, so the handler always reads fresh props and state without triggering effect re-runs.

ts
export type UseEffectEventResult<T extends (...args: any[]) => any> = T;

Released under the MIT License.