useStableHandler (only use when you know what you are doing, or you will be fired)

Exports Size
loading...
Gzip Size
loading...
Brotli Size
Source Code
View on GitHub
Docs
Edit this page

A userland implementation of React 18.3 useEffectEvent with the returned function memoized.

Rules

⚠️

Failed to comply will result in you being fired, literally.

The hook IS ONLY FOR:

The hook IS NOT FOR:

Difference with React's useEffectEvent

  • useEffectEvent is implemented in React, while useStableHandler is implemented in userland.
  • The returned function of useStableHandler is always memoized, while the return value of useEffectEvent is guaranteed to not be memoized at all
  • The returned function of useEffectEvent can not be called during the render phase.
    • Although the returned function useStableHandler should not be called during the render phase as well, it is implemented in a way to only prevent invocation during the render phase before the first mount.
    • You will also be fired if you try to exploit this implementation detail.

When you don't need useStableHandler

You don't necessarily need useStableHandler if you are just updating a state based on other states or current props.

See this GitHub commit sukkaw/foxact @ 5934ed8 (opens in a new tab) for an example, where I drop the useStableHandler from foxact's useUncontrolled.

Usage

import { useStableHandler } from 'foxact/use-stable-handler-only-when-you-know-what-you-are-doing-or-you-will-be-fired';

The detailed usage and the example are intentionally redacted.