oxc-project/oxc · error · OxcDiagnostic

Do not use `setState` in `componentWillUpdate`.

Error message

Do not use `setState` in `componentWillUpdate`.

What it means

Raised by react/no_will_update_set_state when a `setState` call occurs inside `componentWillUpdate`. At the throw site updating state during the update phase forces React to re-render in the same pass, which is disallowed and leads to render loops or the notorious 'Cannot update during an existing state transition' warning. run() finds setState calls within the body of componentWillUpdate methods (checked only for class components per React version config) and reports the call site.

Source

Thrown at crates/oxc_linter/src/rules/react/no_will_update_set_state.rs:17

use oxc_ast::{AstKind, ast::Expression};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use serde::Deserialize;

use crate::{
    AstNode,
    config::ReactVersion,
    context::LintContext,
    rule::{DefaultRuleConfig, Rule},
    rules::ContextHost,
    utils::{AllowedOrDisallowInFunc, is_es5_component, is_es6_component},
};

fn no_will_update_set_state_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Do not use `setState` in `componentWillUpdate`.")
        .with_help("Updating state during the update step can lead to indeterminate component state and is not allowed.")
        .with_label(span)
}

#[derive(Debug, Default, Clone, Deserialize)]
pub struct NoWillUpdateSetState(AllowedOrDisallowInFunc);

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallows using `setState` in the `componentWillUpdate` lifecycle method.
    ///
    /// ### Why is this bad?
    ///
    /// Updating the state during the component update step can lead to indeterminate component state and is not allowed.
    /// This can cause unexpected behavior and bugs in your React application.
    ///
    /// ### Examples

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Move the state update to componentDidUpdate or derive state via getDerivedStateFromProps.
  2. Compute the value during render instead of storing it in state.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/react/no_will_update_set_state.rs:17 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/7005eb0edbcd4b42. Report an issue: GitHub.