rerender-lazy-state-init — Claude Code rule
The rerender-lazy-state-init rule recommends lazy useState initialization for costly starting values. It helps when a React component reads stored settings or builds a data structure.
MIT ★ 7,191 updated 2026-10-24
.claude/rules/rerender-lazy-state-init.mdWhat rerender-lazy-state-init does
With useState(buildSearchIndex(items)), the function call is evaluated on each render, even though React uses that value only to initialize state. The rule shows the function form, useState(() => buildSearchIndex(items)), to defer the calculation. The same pattern applies when parsing settings from localStorage.
Use lazy initialization for values read from localStorage or sessionStorage, pulled from the DOM, or produced by heavy transformations. Simple values such as useState(0) do not need it. That distinction matters: a direct expensive call runs again on subsequent renders. For Claude Code, add .claude/rules/rerender-lazy-state-init.md to the project rules.
What it helps with
- avoid rebuilding a search index on each render
- defer parsing stored settings until state initialization
- prevent a costly transformation from running after every component update
Example prompts for the agent
- Check this component and move search-index construction into a lazy
useStateinitializer. - Find expensive state initializers and switch direct calls to the function form.
- Review whether settings from
localStorageare being read on every render.
How to install rerender-lazy-state-init
Manually: put the files in place
.claude/rules/rerender-lazy-state-init.mdThe paths frontmatter field limits the files
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.cursor/rules/rerender-lazy-state-init.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/rerender-lazy-state-init.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/rerender-lazy-state-init.mdManually: put the files in place
.clinerules/rerender-lazy-state-init.mdStep by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use
Questions about rerender-lazy-state-init
How do I install rerender-lazy-state-init in Claude Code?
Add the rule file at .claude/rules/rerender-lazy-state-init.md.
What does rerender-lazy-state-init do?
It recommends passing a function to useState when computing the initial value is expensive. React then uses that initializer for the initial render rather than evaluating the expression on every render.
Should I use lazy initialization for useState(0)?
No. The rule says the function form is unnecessary for simple primitives and cheap literals.
Source text
rerender-lazy-state-init.md · MIT
---
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.
© tech-leads-club · MIT · tech-leads-club/agent-skills