noctcore-react/no-effect-derived-state
noctcore-react/no-effect-derived-state
- Package
- react v1.1.0
- Preset
- recommended
- Severity
- error
- Autofix
- No
- Suggestions
- No
- Options
- None
- Type info
- Not needed
An effect that only sets state derived from its dependencies is the “you might not need an effect” anti-pattern.
An effect whose only job is to setState a value purely derived from its own dependencies should
not exist — the value should be computed during render (or with useMemo). The effect version runs
an extra render pass, flashes stale UI for a frame, and is a common source of update loops. See the
React docs, “You Might Not Need an Effect”.
Severity
Section titled “Severity”Ships as error in the recommended preset. It is a heuristic, which is an argument for keeping
it narrow rather than advisory: it fires only on an effect whose whole body is setX(...) of
values read from its own deps, and anything with a branch, a call or a cleanup bails out
unflagged.
What it flags
Section titled “What it flags”This rule is deliberately conservative — it fires only on the unambiguous shape:
- a
useEffect/useLayoutEffectwith a dependency array; - a non-async callback whose body is nothing but
setX(...)statements, where eachsetXis a setter destructured fromuseState/useReducerin scope (resolved through scope analysis, not by its name); - every setter argument is a pure expression (no calls, awaits,
new, JSX, or assignments) whose identifiers all resolve to a dependency; - at least one argument actually reads a dependency.
// derived state synced through an effectconst [fullName, setFullName] = useState('');useEffect(() => { setFullName(firstName + ' ' + lastName);}, [firstName, lastName]);// compute during renderconst fullName = firstName + ' ' + lastName;What it does not flag
Section titled “What it does not flag”Anything with a branch, a side effect, an await, a cleanup, or an argument that reaches outside the dependency array bails out unflagged:
- a setter argument that is a call (
setX(compute(a))), which could be impure; - an effect with no dependency array, or an
asynccallback; - a constant initialiser (
setX(0)) that reads no dependency; - an
if, a cleanupreturn, or any statement besides the setters; - a function that only looks like a setter: one from a context hook or a prop
(
const { setPageTitle } = usePageMeta()), which syncs an external store, andsetTimeout/setInterval; - prop-to-state with local edits:
const [draft, setDraft] = useState(value)re-synced byuseEffect(() => { setDraft(value); }, [value]). The state is seeded from the same dependency, so the effect resets a local draft rather than deriving a value.
When not to use it
Section titled “When not to use it”If you rely on the extra render pass an effect gives you (rare, and usually a smell), disable it.