rerender-functional-setstate
Use functional setState updates to ensure components always reference the latest state and prevent unnecessary callback recreations.
Install
mkdir -p .claude/skills/rerender-functional-setstate && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/3352" && unzip -o skill.zip -d .claude/skills/rerender-functional-setstate && rm skill.zipInstalls to .claude/skills/rerender-functional-setstate
Activation
This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.
Use functional setState updates to prevent stale closures and unnecessary callback recreations. Apply when updating state based on the current state value in React components.Key capabilities
- →Convert direct state references to updater functions
- →Eliminate stale closures in event callbacks
- →Remove unnecessary dependency arrays in useCallback
- →Ensure stable callback references for memoized components
How it works
Applies a pattern transformation to replace state-dependent callbacks with functional updaters that accept the current state value directly.
Inputs & outputs
When to use rerender-functional-setstate
- →Fixing stale state in event handlers
- →Optimizing child component re-renders
- →Refactoring complex state setters
About this skill
Use Functional setState Updates
When updating state based on the current state value, use the functional update form of setState instead of directly referencing the state variable. This prevents stale closures, eliminates unnecessary dependencies, and creates stable callback references.
Incorrect (requires state as dependency):
function TodoList() {
const [items, setItems] = useState(initialItems)
// Callback must depend on items, recreated on every items change
const addItems = useCallback((newItems: Item[]) => {
setItems([...items, ...newItems])
}, [items]) // items dependency causes recreations
// Risk of stale closure if dependency is forgotten
const removeItem = useCallback((id: string) => {
setItems(items.filter(item => item.id !== id))
}, []) // Missing items dependency - will use stale items!
return <ItemsEditor items={items} onAdd={addItems} onRemove={removeItem} />
}
The first callback is recreated every time items changes, which can cause child components to re-render unnecessarily. The second callback has a stale closure bug—it will always reference the initial items value.
Correct (stable callbacks, no stale closures):
function TodoList() {
const [items, setItems] = useState(initialItems)
// Stable callback, never recreated
const addItems = useCallback((newItems: Item[]) => {
setItems(curr => [...curr, ...newItems])
}, []) // No dependencies needed
// Always uses latest state, no stale closure risk
const removeItem = useCallback((id: string) => {
setItems(curr => curr.filter(item => item.id !== id))
}, []) // Safe and stable
return <ItemsEditor items={items} onAdd={addItems} onRemove={removeItem} />
}
Benefits:
- Stable callback references - Callbacks don't need to be recreated when state changes
- No stale closures - Always operates on the latest state value
- Fewer dependencies - Simplifies dependency arrays and reduces memory leaks
- Prevents bugs - Eliminates the most common source of React closure bugs
When to use functional updates:
- Any setState that depends on the current state value
- Inside useCallback/useMemo when state is needed
- Event handlers that reference state
- Async operations that update state
When direct updates are fine:
- Setting state to a static value:
setCount(0) - Setting state from props/arguments only:
setName(newName) - State doesn't depend on previous value
Note: If your project has React Compiler enabled, the compiler can automatically optimize some cases, but functional updates are still recommended for correctness and to prevent stale closure bugs.
When not to use it
- →Simple state updates that do not depend on the previous value
- →Projects using Redux/Context instead of local useState
Limitations
- →Does not improve state logic complexity itself
- →Changes the way state is accessed during the render cycle
How it compares
Prioritizes architectural stability by removing dependencies that trigger unnecessary component re-renders.
Compared to similar skills
rerender-functional-setstate side by side with the closest alternatives in the catalog.
| Skill | Installs | Updated | Safety | Difficulty |
|---|---|---|---|---|
| rerender-functional-setstate (this skill) | 1 | 6mo | No flags | Beginner |
| code-standards | 2 | 5mo | Review | Beginner |
| js-tosorted-immutable | 1 | 6mo | No flags | Beginner |
| writing-react-effects | 1 | 3mo | No flags | Intermediate |
Try saying
Example prompts that trigger this skill in your AI assistant.
More by TheOrcDev
View all by TheOrcDev →You might also like
code-standards
redpanda-data
TypeScript, React, and JavaScript best practices enforced by Ultracite/Biome.
js-tosorted-immutable
TheOrcDev
Use toSorted() instead of sort() to avoid mutating arrays. Apply when sorting arrays that are React props, state, or otherwise shared/referenced elsewhere.
writing-react-effects
dust-tt
Writes React components without unnecessary useEffect. Use when creating/reviewing React components, refactoring effects, or when code uses useEffect to transform data or handle events.
tsh-writing-hooks
TheSoftwareHouse
Custom hook and composable patterns — naming, composition, stable return shapes, lifecycle cleanup, and testing strategies. Use when writing reusable logic units (React hooks, Vue composables), refactoring logic into hooks, debugging hook behavior, or reviewing hook implementations.
accessibility-compliance
wshobson
Implement WCAG 2.2 compliant interfaces with mobile accessibility, inclusive design patterns, and assistive technology support. Use when auditing accessibility, implementing ARIA patterns, building for screen readers, or ensuring inclusive user experiences.
react-modernization
wshobson
Upgrade React applications to latest versions, migrate from class components to hooks, and adopt concurrent features. Use when modernizing React codebases, migrating to React Hooks, or upgrading to latest React versions.