oxc-project/oxc · error · OxcDiagnostic

Unsafe lifecycle method

Error message

Unsafe lifecycle method `{method_name}` is not allowed

What it means

Raised by react/no_unsafe when a class declares a legacy unsafe lifecycle method (`componentWillMount`, `componentWillReceiveProps`, `componentWillUpdate`, or their UNSAFE_-prefixed forms). At the throw site the method is scheduled for removal/already unsupported under async rendering (React 16.3+/17+ with StrictMode double-invocation), and the message indicates the supported replacement (componentDidMount, getDerivedStateFromProps, componentDidUpdate). no_unsafe_diagnostic fires from run() for each matching class method, gated by the configured React version.

Solutions

  1. Migrate componentWillMount to componentDidMount.
  2. Migrate componentWillReceiveProps to getDerivedStateFromProps.
  3. Migrate componentWillUpdate to componentDidUpdate or getSnapshotBeforeUpdate.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/react/no_unsafe.rs:26 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/4091d74abf38cb69. Report an issue: GitHub.

Appendix: source

Thrown at crates/oxc_linter/src/rules/react/no_unsafe.rs:26

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

fn no_unsafe_diagnostic(method_name: &str, span: Span) -> OxcDiagnostic {
    let replacement = match method_name {
        "componentWillMount" | "UNSAFE_componentWillMount" => "componentDidMount",
        "componentWillReceiveProps" | "UNSAFE_componentWillReceiveProps" => {
            "getDerivedStateFromProps"
        }
        "componentWillUpdate" | "UNSAFE_componentWillUpdate" => "componentDidUpdate",
        _ => "alternative lifecycle methods",
    };

    OxcDiagnostic::warn(format!("Unsafe lifecycle method `{method_name}` is not allowed"))
        .with_help(format!(
            "Use `{replacement}` instead. See https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html"
        ))
        .with_label(span)
}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, Default)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
struct NoUnsafeConfig {
    /// Whether to check for the non-prefixed lifecycle methods.
    /// If `true`, this means `componentWillMount`, `componentWillReceiveProps`,
    /// and `componentWillUpdate` will also be flagged, rather than just the
    /// UNSAFE_ versions. It is recommended to set this to `true` to fully
    /// avoid unsafe lifecycle methods.
    check_aliases: bool,
}

#[derive(Debug, Default, Clone, Deserialize, Serialize, JsonSchema)]

View on GitHub (pinned to e1e7af627c)