* refactor: consolidate agent config folders into agents directory - Move .claude/skills and .claude/rules content to agents/ - Remove duplicate .cursor/ and .goose/ folders - Create symlinks from .claude/ and .cursor/ to agents/ - Convert review.mdc to quality-review-checklist.md with proper frontmatter Co-Authored-By: Claude Haiku 4.5 <[email protected]> * feat: add Cal.com APIv2 skills to agents directory Move the Cal.com API v2 skills from PR #27445 into the consolidated agents/skills/ directory structure. This includes: - SKILL.md - Main skill file with API overview - references/authentication.md - Authentication methods - references/bookings.md - Bookings API reference - references/calendars.md - Calendars API reference - references/event-types.md - Event types API reference - references/schedules.md - Schedules API reference - references/slots-availability.md - Slots and availability API reference - references/webhooks.md - Webhooks API reference Co-Authored-By: [email protected] <[email protected]> * fix: update calcom-api skill to use Claude Code frontmatter format Remove non-standard frontmatter fields (license, metadata) and keep only the Claude Code supported fields (name, description) as per the Claude Code skills specification. Co-Authored-By: [email protected] <[email protected]> * refactor: restructure calcom-api SKILL.md as index file Convert SKILL.md from a comprehensive API doc (317 lines) to a concise index file (108 lines) that references the detailed documentation in the references/ folder. This follows the Claude Code skills pattern of keeping SKILL.md focused with supporting files for detailed content. The SKILL.md now: - Provides a quick start guide with essential examples - References all 7 detailed reference docs in a table - Lists common workflows and best practices - Points to external resources Co-Authored-By: [email protected] <[email protected]> * docs: preserve deleted content in reference files Move content that was removed from SKILL.md to appropriate reference files: - Add Error Handling and Pagination sections to authentication.md - Add Organization endpoints to event-types.md - Add Core Concepts section back to SKILL.md This ensures no useful API documentation is lost during the restructuring. Co-Authored-By: [email protected] <[email protected]> --------- Co-authored-by: Claude Haiku 4.5 <[email protected]> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
75 lines
2.9 KiB
Markdown
75 lines
2.9 KiB
Markdown
---
|
|
title: Use Functional setState Updates
|
|
impact: MEDIUM
|
|
impactDescription: prevents stale closures and unnecessary callback recreations
|
|
tags: react, hooks, useState, useCallback, callbacks, closures
|
|
---
|
|
|
|
## 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):**
|
|
|
|
```tsx
|
|
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):**
|
|
|
|
```tsx
|
|
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](https://react.dev/learn/react-compiler) enabled, the compiler can automatically optimize some cases, but functional updates are still recommended for correctness and to prevent stale closure bugs.
|