Skip to content

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

Open

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

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