New
Introducing React Bench, see how different models perform on React code

react-doctor/no-side-effect-in-state-updater-function

Side effect inside a state updater function

  • Category: Bugs
  • Severity: warn
  • Source: oxlint-plugin-react-doctor
  • Framework: global
  • Enabled when: react
  • Default: Enabled

Validate the diagnostic

Confirm the reported code matches this rule before you edit it.

Confirm the reported code matches react-doctor/no-side-effect-in-state-updater-function. Compare it with the Before example and verify the same API, framework, and execution context. Treat it as a false positive only when one of those conditions differs.

Compare the code

The corrected pattern shows a focused way to address the diagnostic.

Reported pattern

const List = ({ nextItem }) => {
  const [items, setItems] = useState([]);
  const addItem = () => {
    setItems((currentItems) => {
      analytics.track("added");
      return [...currentItems, nextItem];
    });
  };
  return <button onClick={addItem}>{items.length}</button>;
};

Corrected pattern

const List = ({ nextItem }) => {
  const [items, setItems] = useState([]);
  const addItem = () => {
    analytics.track("added");
    setItems((currentItems) => [...currentItems, nextItem]);
  };
  return <button onClick={addItem}>{items.length}</button>;
};

How to fix

Use the corrected pattern as a reference. Preserve behavior that the rule does not require you to change.

Copyable fix prompt

Copy this self-contained prompt into your coding agent after you confirm the diagnostic.

Fix every confirmed react-doctor/no-side-effect-in-state-updater-function diagnostic in the current repository. Required change: - React may replay a state updater, so callbacks, analytics, and persistence inside it can run more than once. Compute state purely, then perform the side effect outside the setter.
Reference transformation:Before:
const List = ({ nextItem }) => {
  const [items, setItems] = useState([]);
  const addItem = () => {
    setItems((currentItems) => {
      analytics.track("added");
      return [...currentItems, nextItem];
    });
  };
  return <button onClick={addItem}>{items.length}</button>;
};
After:
const List = ({ nextItem }) => {
  const [items, setItems] = useState([]);
  const addItem = () => {
    analytics.track("added");
    setItems((currentItems) => [...currentItems, nextItem]);
  };
  return <button onClick={addItem}>{items.length}</button>;
};
Constraints: - Confirm the reported code matches the Before pattern. - Make the smallest change that fixes the root cause. - Preserve behavior and interfaces unrelated to this diagnostic. - Reuse existing project components, utilities, and conventions. - Do not introduce render-phase side effects, render-phase state updates, or Hooks rule violations. - Adapt identifiers and framework details instead of copying blindly. - Do not disable the rule or suppress matching code. - Confirm this rule is enabled for the project: react. Verification: - Run focused tests for the changed behavior. - Run React Doctor and confirm this diagnostic no longer appears. - Report the files changed and any checks you could not run.

More Bugs rules from the rules reference: