noctcore-react/prefer-lazy-state-init
noctcore-react/prefer-lazy-state-init
- Package
- react v1.1.0
- Preset
- recommended
- Severity
- error
- Autofix
--fix- Suggestions
- No
- Options
- Optional
- Type info
- Not needed
Wrap an expensive useState initializer call in a lazy function so it runs once, not every render.
🔧 This rule is automatically fixable.
useState(expensiveCall()) evaluates its initializer on every render. React only keeps the
first result, so every render after the first pays the cost for nothing. Wrapping the initializer in
a function — useState(() => expensiveCall()) — makes React call it once, lazily, on mount.
What it flags
Section titled “What it flags”A useState call (or React.useState) whose first argument is a call expression whose callee
path is listed in initializers. By default that is JSON.parse, localStorage.getItem, and
sessionStorage.getItem. A callee is matched by its dotted path (localStorage.getItem) or bare
name (buildInitialState). The call is found under a TypeScript wrapper (JSON.parse(raw) as Foo,
!, satisfies) and on the left of ?? / || (JSON.parse(raw) ?? {}), since it still runs on
every render there; the fix wraps the whole argument. A call on the right of ?? / || runs only
when the left side is empty and is not reported.
// runs JSON.parse on every renderconst [state, setState] = useState(JSON.parse(raw));// lazy: runs once on mountconst [state, setState] = useState(() => JSON.parse(raw));Autofix
Section titled “Autofix”The autofix wraps the argument in () => ....
What it does not flag
Section titled “What it does not flag”Anything already wrapped in a function is left alone. So are initializers that are not a listed
call: a literal (useState(0)), an identifier or member access (useState(props.initial)), and a
call whose callee is not in initializers (useState(compute())).
Options
Section titled “Options”| Option | Type | Default | Meaning |
|---|---|---|---|
initializers |
string[] |
['JSON.parse', 'localStorage.getItem', 'sessionStorage.getItem'] |
Callee paths whose call in a useState initializer must be made lazy. Add your own expensive builders. |
'noctcore-react/prefer-lazy-state-init': ['error', { initializers: ['JSON.parse', 'crypto.randomUUID', 'buildInitialState'],}]When not to use it
Section titled “When not to use it”If your initializers list only contains cheap calls, lazy init adds noise for no benefit — narrow
the list or disable the rule.