Skip to main content
rule CX Codex

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

Install into Claude Code · Plugin marketplace All methods and platforms ↓
/plugin marketplace add fcakyon/claude-codex-settings
/plugin install react-skills@claude-settings
Open on GitHub

What 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 onLayout handler 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-settings

2Manually: put the files in place

.claude/rules/react-state-dispatcher.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 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

Similar IDE rules

All IDE rules for Frontend and UI →

From the same set113

The whole set →
ui-styling ui-styling enforces modern React Native styling practices: smooth continuous borders, gap spacing, CSS boxShadow syntax, and native gradients. CX Codex +1 ★ 1,216repo stars ui-measure-views ui-measure-views guides agents to measure React Native view dimensions using useLayoutEffect alongside onLayout without triggering unneeded re-renders. CX Codex +1 ★ 1,216repo stars create-issue create-issue is a Claude Code skill for drafting or revising issues about bugs and missing features. It can also refine an issue title or body before publication. CX Codex +1 ★ 1,216repo stars list-performance-item-memo list-performance-item-memo — a rule for React list rows: split the item into primitive props so memo() can compare them shallowly and skip a render when nothing changed. CX Codex +1 ★ 1,216repo stars setup Skill for Claude Code configures real integration testing and user QA journeys across project codebases. It triggers when asked to set up QA or generate end-to-end testing flows. CX Codex +1 ★ 1,216repo stars list-performance-inline-objects list-performance-inline-objects — a rule that prevents creating objects inside renderItem for props. Inline objects create new references on every render and break memoization. CX Codex +1 ★ 1,216repo stars setup setup configures Tavily MCP server credentials inside Claude Code and saves them to the plugin configuration. CX Codex +1 ★ 1,216repo stars bundle-analyzable-paths The bundle-analyzable-paths rule recommends making import and file paths statically analyzable. That keeps bundlers from treating a broad set of files as reachable. CX Codex +1 ★ 1,216repo stars

Project sponsors

Companies that keep this analytics open