Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply 62 React and Next.js performance optimization rules from Vercel Engineering
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
rules/advanced-effect-event-deps.md
1---2title: Do Not Put Effect Events in Dependency Arrays3impact: LOW4impactDescription: avoids unnecessary effect re-runs and lint errors5tags: advanced, hooks, useEffectEvent, dependencies, effects6---78## Do Not Put Effect Events in Dependency Arrays910Effect Event functions do not have a stable identity. Their identity intentionally changes on every render. Do not include the function returned by `useEffectEvent` in a `useEffect` dependency array. Keep the actual reactive values as dependencies and call the Effect Event from inside the effect body or subscriptions created by that effect.1112**Incorrect (Effect Event added as a dependency):**1314```tsx15import { useEffect, useEffectEvent } from 'react'1617function ChatRoom({ roomId, onConnected }: {18roomId: string19onConnected: () => void20}) {21const handleConnected = useEffectEvent(onConnected)2223useEffect(() => {24const connection = createConnection(roomId)25connection.on('connected', handleConnected)26connection.connect()2728return () => connection.disconnect()29}, [roomId, handleConnected])30}31```3233Including the Effect Event in dependencies makes the effect re-run every render and triggers the React Hooks lint rule.3435**Correct (depend on reactive values, not the Effect Event):**3637```tsx38import { useEffect, useEffectEvent } from 'react'3940function ChatRoom({ roomId, onConnected }: {41roomId: string42onConnected: () => void43}) {44const handleConnected = useEffectEvent(onConnected)4546useEffect(() => {47const connection = createConnection(roomId)48connection.on('connected', handleConnected)49connection.connect()5051return () => connection.disconnect()52}, [roomId])53}54```5556Reference: [React useEffectEvent: Effect Event in deps](https://react.dev/reference/react/useEffectEvent#effect-event-in-deps)57