npx skills add smithery/theorcdev --skill rerender-functional-setstate
theorcdev/8bitcn-ui
rerender-functional-setstate
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.
Installation
npx skills add theorcdev/8bitcn-ui --skill rerender-functional-setstate
Similar popular skills
Related neighbors and high-traction skills in the same topics — useful to compare before installing.
Help users work effectively across functions. Use when someone is navigating PM-engineering rel…
1.6K installsUse when designing a Go constructor or factory function with optional configuration — especiall…
1.0K installsAlso in this package
Other skills from theorcdev/8bitcn-ui · top by installs.
npx skills add theorcdev/8bitcn-ui
More details
Agent compatibility
Declared targets from SKILL.md / docs. Unmarked agents are not listed — the skill may still install via the CLI.
Also listed on
Alternate registries and mirrors of this skill.
Repository health
main
Package contents
Files included with this skill beyond the listing page.
-
skill md
SKILL.md2,967 B -
docs
SUMMARY.md211 B
History
- First seen on skills.sh
- First recorded snapshot · 35 installs
SKILL.md
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.