Skip to main content
rule CX Codex

advanced-use-latest — Claude Code rule: useEffectEvent callbacks

advanced-use-latest — a Claude rule file on reaching fresh callback values inside a React effect and keeping the dependency array down to state.

MIT ★ 7,191 updated 2026-10-24

Install into Claude Code · Manually: put the files in place All methods and platforms ↓
.claude/rules/advanced-use-latest.md
Open on GitHub

What advanced-use-latest does

The rule covers a single React pattern: wrap a callback in useEffectEvent so the effect can call its newest copy. Re-runs then depend only on the values the effect really reads.

Its example is a search field with a 300 ms debounce. In the broken version onSearch sits in the dependency list next to query, so the timer restarts each time the parent hands over a new function. The fixed version keeps onSearchEvent and leaves query alone in the deps.

The file is marked LOW impact: a small local edit, nothing structural. It expects useEffectEvent to come from react, so a project on a version that does not export the hook will fail to compile the snippet.

What it helps with

  • drop a callback from an effect's dependency array
  • stop an effect from re-running each time the handler changes
  • avoid a stale closure inside a debounce timer
  • port a debounce to useEffectEvent

Example prompts for the agent

  • Rewrite this effect with useEffectEvent so onSearch stays out of the dependency list.
  • Look at SearchInput: the callback in deps makes the timer restart on every render.
  • Show me how to keep only query in deps and still call the current handler.

How to install advanced-use-latest

Manually: put the files in place

.claude/rules/advanced-use-latest.md

The paths frontmatter field limits the files

Step by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use

Questions about advanced-use-latest

How do I install advanced-use-latest in Claude Code?

Put the rule file at .claude/rules/advanced-use-latest.md inside your project.

What does advanced-use-latest do?

It explains how to wrap a handler in useEffectEvent, so an effect calls the latest version of it and keeps that handler out of the dependency array.

What does the rule expect from the project?

useEffectEvent imported from react. On a React version that lacks the hook, the sample will not compile.

Source text

advanced-use-latest.md · MIT
---
title: useEffectEvent for Stable Callback Refs
impact: LOW
impactDescription: prevents effect re-runs
tags: advanced, hooks, useEffectEvent, refs, optimization
---

## useEffectEvent for Stable Callback Refs

Access latest values in callbacks without adding them to dependency arrays. Prevents effect re-runs while avoiding stale closures.

**Incorrect (effect re-runs on every callback change):**

```tsx
function SearchInput({ onSearch }: { onSearch: (q: string) => void }) {
  const [query, setQuery] = useState('')

  useEffect(() => {
    const timeout = setTimeout(() => onSearch(query), 300)
    return () => clearTimeout(timeout)
  }, [query, onSearch])
}
```

**Correct (using React's useEffectEvent):**

```tsx
import { useEffectEvent } from 'react';

function SearchInput({ onSearch }: { onSearch: (q: string) => void }) {
  const [query, setQuery] = useState('')
  const onSearchEvent = useEffectEvent(onSearch)

  useEffect(() => {
    const timeout = setTimeout(() => onSearchEvent(query), 300)
    return () => clearTimeout(timeout)
  }, [query])
}
```

© tech-leads-club · MIT · tech-leads-club/agent-skills

Similar IDE rules

All IDE rules for Frontend and UI →

From the same set130

The whole set →
rerender-simple-expression-in-memo The rerender-simple-expression-in-memo rule advises against using useMemo for simple expressions that return a boolean, number, or string. It applies when reviewing React component calculations. CX Codex +1 ★ 7,191repo stars js-hoist-regexp js-hoist-regexp — a Claude rule that keeps RegExp construction out of the render path: define the pattern at module scope or wrap it in useMemo(). CX Codex +1 ★ 7,191repo stars rendering-hoist-jsx rendering-hoist-jsx extracts static JSX elements outside components to prevent element re-creation on every render pass. CX Codex +1 ★ 7,191repo stars rerender-transitions The rerender-transitions rule marks frequent, non-urgent state changes as transitions. It fits React code where updates such as scroll tracking should keep the interface responsive. CX Codex +1 ★ 7,191repo stars async-api-routes async-api-routes prevents sequential waterfall requests in API routes and Server Actions. It instructs the assistant to launch independent promises immediately rather than chaining awaits. CX Codex +1 ★ 7,191repo stars js-early-exit The js-early-exit rule recommends returning as soon as a function’s result is clear. It fits checks where continuing after the first error serves no purpose. CX Codex +1 ★ 7,191repo stars rerender-memo rerender-memo — a rule that guides your agent to extract expensive computations into memoized components, enabling early returns without triggering child logic. Speeds up React re-render paths. CX Codex +1 ★ 7,191repo stars bundle-preload bundle-preload directs the agent to preload heavy JavaScript chunks on user intent like hover or focus. This cuts perceived interface latency. CX Codex +1 ★ 7,191repo stars

Project sponsors

Companies that keep this analytics open