- docs/agent/external: Vercel react-best-practices and composition-patterns (MIT), Addy Osmani accessibility (MIT), Anthropic frontend-design (Apache-2.0), official Payload skill at v3.92.1 (MIT) — text only, each with its licence, SOURCE.md (repo, commit) and an override note in front of the SKILL.md (ipal-kit first, Payload fetching over swr, DESIGN.md tokens, Payload 3 not 4); manifest.json with sha256 per file - scripts/vendor-external-skills.mjs (maintainer, not shipped): fetches by full commit SHA only, rejects non-text or executable files, regenerates the manifest; --check verifies it - ipal-skills --external [names]: copies ext-* after checking every file against the manifest, downloads nothing, never touches ext-* folders without ipal-kit's SOURCE.md, removes only retired ones it installed; an unknown name stops the run before anything is copied - AGENT.md §14, ipal-setup stage 2 (optional step), README: usage, overrides, how to update the pins - test/externalSkills.test.ts: pins are SHAs, no branch fetch, checksums, text only, licences, override note, installer leaves other folders alone Co-Authored-By: Claude Opus 5.5 <[email protected]>
59 lines
2.0 KiB
Markdown
59 lines
2.0 KiB
Markdown
---
|
|
title: Use Lazy State Initialization
|
|
impact: MEDIUM
|
|
impactDescription: wasted computation on every render
|
|
tags: react, hooks, useState, performance, initialization
|
|
---
|
|
|
|
## Use Lazy State Initialization
|
|
|
|
Pass a function to `useState` for expensive initial values. Without the function form, the initializer runs on every render even though the value is only used once.
|
|
|
|
**Incorrect (runs on every render):**
|
|
|
|
```tsx
|
|
function FilteredList({ items }: { items: Item[] }) {
|
|
// buildSearchIndex() runs on EVERY render, even after initialization
|
|
const [searchIndex, setSearchIndex] = useState(buildSearchIndex(items))
|
|
const [query, setQuery] = useState('')
|
|
|
|
// When query changes, buildSearchIndex runs again unnecessarily
|
|
return <SearchResults index={searchIndex} query={query} />
|
|
}
|
|
|
|
function UserProfile() {
|
|
// JSON.parse runs on every render
|
|
const [settings, setSettings] = useState(
|
|
JSON.parse(localStorage.getItem('settings') || '{}')
|
|
)
|
|
|
|
return <SettingsForm settings={settings} onChange={setSettings} />
|
|
}
|
|
```
|
|
|
|
**Correct (runs only once):**
|
|
|
|
```tsx
|
|
function FilteredList({ items }: { items: Item[] }) {
|
|
// buildSearchIndex() runs ONLY on initial render
|
|
const [searchIndex, setSearchIndex] = useState(() => buildSearchIndex(items))
|
|
const [query, setQuery] = useState('')
|
|
|
|
return <SearchResults index={searchIndex} query={query} />
|
|
}
|
|
|
|
function UserProfile() {
|
|
// JSON.parse runs only on initial render
|
|
const [settings, setSettings] = useState(() => {
|
|
const stored = localStorage.getItem('settings')
|
|
return stored ? JSON.parse(stored) : {}
|
|
})
|
|
|
|
return <SettingsForm settings={settings} onChange={setSettings} />
|
|
}
|
|
```
|
|
|
|
Use lazy initialization when computing initial values from localStorage/sessionStorage, building data structures (indexes, maps), reading from the DOM, or performing heavy transformations.
|
|
|
|
For simple primitives (`useState(0)`), direct references (`useState(props.value)`), or cheap literals (`useState({})`), the function form is unnecessary.
|