Back to list
TheOrcDev

rerender-functional-setstate

by TheOrcDev

A set of retro-designed, accessible components and a code distribution platform. Open Source. Open Code.

1,530🍴 94📅 Jan 22, 2026

Use Cases

📝

Documentation Generation

Auto-generate documentation from code.

✍️

Content Creation Support

Assist in creating blog posts and marketing content.

🎨

UI Component Generation

Generate UI components from designs.

SKILL.md


name: rerender-functional-setstate description: 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.

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:

  1. Stable callback references - Callbacks don't need to be recreated when state changes
  2. No stale closures - Always operates on the latest state value
  3. Fewer dependencies - Simplifies dependency arrays and reduces memory leaks
  4. 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.

Score

Total Score

95/100

Based on repository quality metrics

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

+10
説明文

100文字以上の説明がある

+10
人気

GitHub Stars 1000以上

+15
最近の活動

1ヶ月以内に更新

+10
フォーク

10回以上フォークされている

+5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

+5

Reviews

💬

Reviews coming soon