rerender-functional-setstate

Convert React setState calls to functional updates to prevent stale closures.

2.0k|117|Updated Mar 27, 2025
One-click install
npx skills add https://github.com/TheOrcDev/8bitcn-ui --skill rerender-functional-setstate
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: rerender-functional-setstate
Source: https://github.com/TheOrcDev/8bitcn-ui/tree/main/.claude/skills/rerender-functional-setstate
Command: npx skills add https://github.com/TheOrcDev/8bitcn-ui --skill rerender-functional-setstate

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

React components often update state based on the current value, which can lead to stale closures and unnecessary callback recreations. This Skill advocates using the functional update form of setState to keep logic correct and performant.

Core Features & Use Cases

  • Stable callbacks: use the functional update form to ensure updates always operate on the latest state.
  • Prevents stale closures: avoids referencing stale state in event handlers, effects, and async logic.
  • Performance benefits: reduces unnecessary re-creations of functions and re-renders by avoiding direct state capture.
  • Use Case: In a Todo list, addItem and removeItem should rely on curr => ... to operate on the freshest items array.

Quick Start

Use the functional update form when updating state that depends on the previous value. For example, replace setItems(items => [...items, newItem]) with setItems(curr => [...curr, newItem]).

Frequently Asked Questions about rerender-functional-setstate

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

FAQPage Schema
How do I prevent stale closures in React state updates?

Prevent stale closures by using the functional update form of setState, where you pass a function that receives the current state instead of relying on captured state variables. For example, use setItems(curr => [...curr, newItem]) rather than setItems([...items, newItem]) to ensure updates always operate on the latest state value.

Why should I use functional setState instead of direct state references?

Functional setState eliminates stale state problems in event handlers, effects, and async callbacks by ensuring each update reads the current state at execution time. This prevents bugs where callbacks capture outdated state values and improves performance by reducing unnecessary function recreations and re-renders.

When do I need functional updates in React components?

Use functional updates whenever state changes depend on the previous value, especially in event handlers, useEffect cleanup, and asynchronous operations. This is critical for features like todo lists, counters, form arrays, and any logic where multiple rapid updates or stale closures could corrupt state.

Can I use stable callbacks with functional setState across effects?

Yes. Functional setState works across useEffect, useCallback, and other hooks by reading current state at update time rather than closure time. This eliminates the need to include state in dependency arrays for callbacks that only update based on the previous state value.

What's the difference between direct state capture and functional updates?

Direct state capture (setItems([...items, newItem])) freezes the state value in the callback closure, causing stale reads if the state changes before the update runs. Functional updates (setItems(curr => [...curr, newItem])) defer the state read until execution, always accessing the current value.