Pulled ALL skills from 15 source repositories: - anthropics/skills: 16 (docs, design, MCP, testing) - obra/superpowers: 14 (TDD, debugging, agents, planning) - coreyhaines31/marketingskills: 25 (marketing, CRO, SEO, growth) - better-auth/skills: 5 (auth patterns) - vercel-labs/agent-skills: 5 (React, design, Vercel) - antfu/skills: 16 (Vue, Vite, Vitest, pnpm, Turborepo) - Plus 13 individual skills from various repos Mosaic Stack is not limited to coding — the Orchestrator and subagents serve coding, business, design, marketing, writing, logistics, analysis, and more. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
92 lines
2.2 KiB
Markdown
92 lines
2.2 KiB
Markdown
---
|
|
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)
|
|
}
|
|
```
|