Awesome Reviewers

Reviewer · React

Avoid derived state in effects

Don't use `useEffect` to compute "derived" values from props/state or to call a state setter that just mirrors another value — these computations should happen directly during render (or memoized with `useMemo`) rather than scheduled in an effect, since the latter causes extra renders and is a common source of bugs.

Instruction

Don’t use useEffect to compute “derived” values from props/state or to call a state setter that just mirrors another value — these computations should happen directly during render (or memoized with useMemo) rather than scheduled in an effect, since the latter causes extra renders and is a common source of bugs.

This rule applies even when the state update is indirect: wrapping the setter in a useEffectEvent, a locally-defined helper function, or a nested/recursive call inside the effect still constitutes “deriving state in an effect” and should be flagged/avoided just like a direct top-level setState(...) call. For example, both of the following are equally problematic:

// Bad: direct derivation in an effect
useEffect(() => {
  setFullName(firstName + ' ' + lastName);
}, [firstName, lastName]);

// Still bad: derivation hidden behind useEffectEvent / a helper function
const onSync = useEffectEvent(() => setFullName(firstName + ' ' + lastName));
useEffect(() => {
  function helper() { onSync(); }
  helper();
}, [firstName, lastName]);

// Good: compute during render
const fullName = firstName + ' ' + lastName;

When implementing or reviewing lint/validation logic for this pattern, make sure the check traces values through intermediate named/unnamed bindings (not just directly-named identifiers) and follows call chains (helper functions, recursive calls, useEffectEvent wrappers) so indirect derivations are caught, not just the obvious top-level case.