From 86e7156c94c5b2bb4a0037d939caeb45691503d1 Mon Sep 17 00:00:00 2001 From: pawannyadav05 Date: Sat, 21 Nov 2026 01:05:51 +0530 Subject: [PATCH] docs: clarify that immutability lint rule does not flag method mutations 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 #8758 --- .../lints/immutability.md | 25 ++++++------------- 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/src/content/reference/eslint-plugin-react-hooks/lints/immutability.md b/src/content/reference/eslint-plugin-react-hooks/lints/immutability.md index 2314cde0649..359bf800505 100644 --- a/src/content/reference/eslint-plugin-react-hooks/lints/immutability.md +++ b/src/content/reference/eslint-plugin-react-hooks/lints/immutability.md @@ -17,16 +17,6 @@ A component’s props and state are immutable snapshots. Never mutate them direc ### Invalid {/*invalid*/} ```js -// ❌ Array push mutation -function Component() { - const [items, setItems] = useState([1, 2, 3]); - - const addItem = () => { - items.push(4); // Mutating! - setItems(items); // Same reference, no re-render - }; -} - // ❌ Object property assignment function Component() { const [user, setUser] = useState({name: 'Alice'}); @@ -36,16 +26,15 @@ function Component() { setUser(user); // Same reference }; } +``` -// ❌ Sort without spreading -function Component() { - const [items, setItems] = useState([3, 1, 2]); + - const sortItems = () => { - setItems(items.sort()); // sort mutates! - }; -} -``` +#### Note on Array and Method Mutations {/*array-and-method-mutations*/} + +Direct property mutations (like `user.name = 'Bob'`) are caught and flagged by this rule. However, calling mutating methods like `items.push(4)` or `items.sort()` will not produce a linter diagnostic unless the compiler can fully infer the state type. Even if the linter does not flag them, you must avoid in-place mutations because passing the same object/array reference to a state setter prevents React from triggering re-renders. + + ### Valid {/*valid*/}