react-state-dispatcher — Claude Code rule: what it does
react-state-dispatcher enforces functional dispatch updaters in React setState calls whenever state depends on current values. Prevents stale closures in callbacks.
MIT ★ 1,216 updated 2026-10-24
/plugin marketplace add fcakyon/claude-codex-settings
/plugin install react-skills@claude-settingsWhat react-state-dispatcher does
This instruction guides the coding agent to emit setState(prev => ...) instead of capturing state variables inside closures. Doing so ensures updates evaluate against the freshest data, especially during asynchronous events or rapid UI triggers like layout changes.
Inside the prompt, the agent receives clear positive and negative patterns. It learns to return prev directly when object properties match, halting unnecessary component renders. At the same time, manual comparison guards are removed for standalone primitive updates where values originate independently of previous state.
What it helps with
- prevent stale closure bugs inside event handlers
- skip redundant component re-renders by returning unchanged references
- refactor direct state reads into functional updater callbacks
Example prompts for the agent
- Refactor the
onLayouthandler to use a functional state updater - Fix the stale closure inside my counter click callback
- Update our React state setter to avoid unnecessary re-renders on layout change
How to install react-state-dispatcher
1Plugin marketplace
/plugin marketplace add fcakyon/claude-codex-settings/plugin install react-skills@claude-settings2Manually: put the files in place
.claude/rules/react-state-dispatcher.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/react-state-dispatcher.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/react-state-dispatcher.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/react-state-dispatcher.mdManually: put the files in place
.clinerules/react-state-dispatcher.mdStep by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use
Questions about react-state-dispatcher
How to install react-state-dispatcher in Claude Code?
Run /plugin marketplace add fcakyon/claude-codex-settings followed by /plugin install react-skills@claude-settings, or place the file at .claude/rules/react-state-dispatcher.md.
What is the primary role of react-state-dispatcher?
It makes the assistant write setState(prev => ...) whenever updating state based on its existing value to avoid stale closures.
Does the rule require comparing primitive state values?
No, it marks manual equality checks on primitive states as incorrect when the incoming value does not depend on current state.
Source text
react-state-dispatcher.md · MIT
---
title: useState Dispatch updaters for State That Depends on Current Value
impact: MEDIUM
impactDescription: avoids stale closures, prevents unnecessary re-renders
tags: state, hooks, useState, callbacks
---
## Use Dispatch Updaters for State That Depends on Current Value
When the next state depends on the current state, use a dispatch updater
(`setState(prev => ...)`) instead of reading the state variable directly in a
callback. This avoids stale closures and ensures you're comparing against the
latest value.
**Incorrect (reads state directly):**
```tsx
const [size, setSize] = useState<Size | undefined>(undefined)
const onLayout = (e: LayoutChangeEvent) => {
const { width, height } = e.nativeEvent.layout
// size may be stale in this closure
if (size?.width !== width || size?.height !== height) {
setSize({ width, height })
}
}
```
**Correct (dispatch updater):**
```tsx
const [size, setSize] = useState<Size | undefined>(undefined)
const onLayout = (e: LayoutChangeEvent) => {
const { width, height } = e.nativeEvent.layout
setSize((prev) => {
if (prev?.width === width && prev?.height === height) return prev
return { width, height }
})
}
```
Returning the previous value from the updater skips the re-render.
For primitive states, you don't need to compare values before firing a
re-render.
**Incorrect (unnecessary comparison for primitive state):**
```tsx
const [size, setSize] = useState<Size | undefined>(undefined)
const onLayout = (e: LayoutChangeEvent) => {
const { width, height } = e.nativeEvent.layout
setSize((prev) => (prev === width ? prev : width))
}
```
**Correct (sets primitive state directly):**
```tsx
const [size, setSize] = useState<Size | undefined>(undefined)
const onLayout = (e: LayoutChangeEvent) => {
const { width, height } = e.nativeEvent.layout
setSize(width)
}
```
However, if the next state depends on the current state, you should still use a
dispatch updater.
**Incorrect (reads state directly from the callback):**
```tsx
const [count, setCount] = useState(0)
const onTap = () => {
setCount(count + 1)
}
```
**Correct (dispatch updater):**
```tsx
const [count, setCount] = useState(0)
const onTap = () => {
setCount((prev) => prev + 1)
}
```
© fcakyon · MIT · fcakyon/claude-codex-settings