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
.claude/rules/advanced-use-latest.mdWhat 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
useEffectEventsoonSearchstays 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
queryin deps and still call the current handler.
How to install advanced-use-latest
Manually: put the files in place
.claude/rules/advanced-use-latest.mdThe paths frontmatter field limits the files
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.cursor/rules/advanced-use-latest.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/advanced-use-latest.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/advanced-use-latest.mdManually: put the files in place
.clinerules/advanced-use-latest.mdStep 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