Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

use-effect-event

sanity-io637.4kMIT2.0.0TypeScript support: included

Ponyfill of the experimental React.useEffectEvent hook

useEffectEvent, useEvent, hooks, react, react18, react19, sanity, sanity-io, typescript, effect, use

readme

CI & Release npm version

use-effect-event

Ponyfill of the experimental React.useEffectEvent hook

Usage

[!IMPORTANT] Make sure you read about the limitations and understand them before you start using this hook, it's not a silver bullet.

This package implements the same API as the experimental React.useEffectEvent hook, based on its implementation in Bluesky. The only difference is that instead of installing an experimental build of React, you can use this package as a ponyfill. Here's an example, from the official docs, that shows how it can be used to log whenever url changes, and still access the latest value of numberOfItems without needing to resort to useRef proxying:

// import {useEffectEvent} from 'react'
import {useEffectEvent} from 'use-effect-event'

function Page({url}) {
  const {items} = useContext(ShoppingCartContext)
  const numberOfItems = items.length

  const onVisit = useEffectEvent((visitedUrl) => {
    logVisit(visitedUrl, numberOfItems)
  })

  useEffect(() => {
    onVisit(url)
  }, [url])
}

Usage with eslint-plugin-react-hooks

In order to use this hook with eslint-plugin-react-hooks, install eslint-plugin-react-hooks@experimental:

  "devDependencies": {
    "eslint-plugin-react-hooks": "experimental"
  }

The experimental version of the react-hooks ESLint plugin checks that Effect Events are used according to the rules:

  1. useEffectEvent functions are not passed between components (react-hooks/rules-of-hooks)
  2. useEffectEvent functions are excluded from the dependency arrays of useEffect calls (react-hooks/exhaustive-deps)
    • this corrects the behavior of the stable version, which erroneously requires that useEffectEvent functions are included as effect dependencies.

changelog

📓 Changelog

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

2.0.0 (2025-05-23)

⚠ BREAKING CHANGES

  • update useEffectEvent to be a better match for the native implementation (#22)

Features

  • update useEffectEvent to be a better match for the native implementation (#22) (23bde11)

Bug Fixes

  • docs: add information about supported version of eslint-plugin-react-hooks (#20) (873e9cb)

1.0.2 (2024-07-10)

Bug Fixes

  • README: update usage snippet (#3) (e277205)

1.0.1 (2024-07-10)

Bug Fixes

  • set publish config to public (0901ef8)

1.0.0 (2024-07-09)

Features