Skip to content

[Bug]: immutability lint page lists push() and sort() mutations that the rule does not report #8758

Description

@alainkaiser

Summary

The immutability page lists three "Invalid" examples. eslint-plugin-react-hooks 7.1.1 reports only the property assignment (user.name = 'Bob'). The items.push(4) and setItems(items.sort()) examples, and the todos.push() example under Troubleshooting, produce no diagnostic.

This is expected behavior per react/react#35157: method calls on state error only when the compiler knows the type, and it does not infer state types. The page presents them as cases the rule catches.

Page

https://react.dev/reference/eslint-plugin-react-hooks/lints/immutability

Details

eslint 10.14.0, eslint-plugin-react-hooks 7.1.1, 'react-hooks/immutability': 'error':

import { useState } from 'react'

export function Component() {
  const [items, setItems] = useState([1, 2, 3])
  const addItem = () => {
    items.push(4)
    setItems(items)
  }
  return <button onClick={addItem}>{items.length}</button>
}

No diagnostic. The same component with user.name = 'Bob'; setUser(user) reports "This value cannot be modified".

Suggested fix: keep the property assignment as the "Invalid" example and move push() and sort() to a note that says the rule cannot detect method-call mutations on state yet (react/react#35157).

Activity

  1. pawannyadav05 commented on Nov 21, 2026

    @pawannyadav05

    Fixes #8758

    Summary

    Updates the immutability ESLint rule reference page to accurately reflect rule diagnostics: retains verified object property assignments under ### Invalid and moves mutating methods (push(), sort()) into an instructional Note explaining compiler inference boundaries.

    Problem

    The immutability rule documentation previously presented three examples under ### Invalid:

    1. items.push(4); setItems(items);
    2. user.name = 'Bob'; setUser(user);
    3. setItems(items.sort());

    As reported in #8758 and tracked in react/react#35157, eslint-plugin-react-hooks only emits diagnostics on direct property assignments (like user.name = 'Bob'). Calling mutating methods on state variables produces no diagnostic unless the compiler can fully infer the state type. Presenting push() and sort() as cases flagged by the rule misleads developers when their linter produces no error.

    Solution

    • Accurate Diagnostic Examples: Kept direct property assignment under ### Invalid as the verified case flagged by the linter.
    • Added Explanatory Note: Introduced a dedicated note explaining that while calling mutating methods like items.push(4) or items.sort() will not trigger a linter diagnostic without inferred types, mutating state in-place is still prohibited because identical references prevent re-renders.

    Changes

    • src/content/reference/eslint-plugin-react-hooks/lints/immutability.md: Refined the Invalid section and added the note on method mutations.

    PR opened in #8769.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions