Effect that copies into state
Effect that copies into state. This effect only copies values into state. Compute them during render instead.
This is how the comment reads inline, under the flagged line.
Why it matters
Setting state from an effect to mirror props or other state costs an extra render and drifts out of sync.
What to do
Compute the value during render (const full = first + ' ' + last), with useMemo if it's expensive.
Examples
useEffect(() => {
setFull(first + ' ' + last);
}, [first, last]);const full = first + ' ' + last;
What it catches
It reads the shape of the code: functions, blocks, loops and calls. Languages: TypeScript and JavaScript. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.
Flagged
useEffect(() => {
setFull(a + b);
}, [a, b]);
Left alone
const full = a + b;
Research
Effects with no dependency array, data fetching in effects, and effects that copy derived values into state. They cause extra renders, loops, races and double fetches.
- X post by aidenybai
- skill: react-doctor
- owner's notes
Turn it off or change it
In a config file for the CLI, or in the API's rules field:
{
"rules": {
"disable": [
"derived-state-effect"
],
"severity": {
"derived-state-effect": "medium"
}
}
}