Skip to content

docs: clarify that immutability lint rule does not flag method mutations without inferred types - #8769

Open
pawannyadav05 wants to merge 1 commit into
reactjs:mainfrom
pawannyadav05:docs/immutability-lint-clarify-push-sort
Open

pawannyadav05 wants to merge 1 commit into
reactjs:mainfrom
pawannyadav05:docs/immutability-lint-clarify-push-sort

Conversation

@pawannyadav05

Copy link
Copy Markdown

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());

However, 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 (which it does not infer for arbitrary state arrays). 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.

…ons without inferred types

The immutability lint documentation previously listed array push() and sort()
under 'Invalid' examples as if the rule flagged them. However, per react/react#35157,
eslint-plugin-react-hooks only produces diagnostics on property assignments unless
the compiler can infer the type.

This updates the examples to present property assignment as the verified invalid
example and adds an explanatory Note explaining why method mutations do not emit
a diagnostic while remaining bad practice.

Fixes reactjs#8758
@github-actions

Copy link
Copy Markdown

Size changes

Details

📦 Next.js Bundle Analysis for react-dev

This analysis was generated by the Next.js Bundle Analysis action. 🤖

This PR introduced no changes to the JavaScript bundle! 🙌

This branch was successfully deployed

1 active deployment
Preview — 86e7156c Deployed Nov 21, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant