strict-mode-updater-safety

Detect and fix impure React setState updater functions exposed by Strict Mode.

Updated Jul 21, 2026
One-click install
npx skills add https://github.com/masakinihirota/2026src-ni --skill strict-mode-updater-safety-masakinihirota
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: strict-mode-updater-safety
Source: https://github.com/masakinihirota/2026src-ni/tree/main/.agents/skills/strict-mode-updater-safety
Command: npx skills add https://github.com/masakinihirota/2026src-ni --skill strict-mode-updater-safety-masakinihirota

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? React Strict Mode double-invokes setState updater functions in development, which exposes hidden bugs where updaters read refs, mutate external variables, or trigger side effects. These bugs cause state inconsistencies that only appear in development or during rapid consecutive interactions, making them hard to reproduce and fix. ## Core Features & Use Cases - Detection Rules: Concrete patterns for spotting impure updaters, including ref reads/writes, external mutable state, non-deterministic values like Date.now or Math.random, and side effects such as logging or API calls inside updaters. - Fix Templates: Three ready-to-apply refactoring templates covering ref capture outside the updater, pre-computing non-deterministic values, and moving side effects into event handlers. - Regression Testing: A minimal Vitest and React Testing Library test pattern that wraps components in StrictMode and verifies consecutive operations produce independent, correct state. - Use Case: A button that adds candidates to a form works once but overwrites state on the second click in development. Use this Skill to write a failing test, extract the ref access out of the updater, and lock in a two-click regression test. ## Quick Start Ask the AI to review my React component for impure setState updater functions and fix them using the strict mode updater safety workflow with a failing test first.

Frequently Asked Questions about strict-mode-updater-safety

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix setState updater bugs in React Strict Mode?▼

Move all ref reads, external mutations, and side effects outside the updater function so it only computes the next state from the previous state. Capture mutable values before calling setState, then write a failing test first and confirm it passes after the refactor.

Why does my React state break only in development mode?▼

React Strict Mode intentionally double-invokes updater functions in development to surface impurity bugs. If your updater reads refs, mutates external variables, or calls Date.now inside the updater, the second invocation produces different results than the first.

How do I test React components with Strict Mode using Vitest?▼

Wrap the component in React's StrictMode when rendering with React Testing Library, then simulate consecutive user actions with userEvent. Assert that both operations produce independent, correct state to lock in the regression.

Can I use refs inside a setState updater function?▼

No, reading or writing refs inside an updater is unsafe because Strict Mode may invoke the updater twice. Capture the ref value before calling setState, clear it if needed, and pass the captured value into the pure updater.

What side effects are not allowed in React state updaters?▼

Updaters must not perform API calls, logging, DOM manipulation, or mutations of external variables. Move these side effects into the event handler that triggers the state update, keeping the updater a pure prev-to-next transformation.