# immutability

Rule `react-hooks/immutability`. Validates against mutating props, state, and other values that are immutable

- Source: react-hooks
- Technologies: React
- Kind: correctness
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 165.2M weekly downloads of presets that enable it
- Trend: +28.2% last quarter
- Rank: #96 by reach, #1345 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `react-hooks/immutability` | yes | safe | package `eslint-plugin-react-hooks` | [docs](https://react.dev/reference/eslint-plugin-react-hooks/lints/immutability) |
| Oxlint | `react/immutability` | no | no | plugin `react`, category correctness | [docs](https://oxc.rs/docs/guide/usage/linter/rules/react/immutability.html) |

## Code examples

### Incorrect: the rule reports this

```js
function Component() {
  const [items, setItems] = useState([1, 2, 3]);

  const addItem = () => {
    items.push(4); // Mutating!
    setItems(items); // Same reference, no re-render
  };
}
```

```js
function Component() {
  const [user, setUser] = useState({name: 'Alice'});

  const updateName = () => {
    user.name = 'Bob'; // Mutating!
    setUser(user); // Same reference
  };
}
```

```js
function Component() {
  const [items, setItems] = useState([3, 1, 2]);

  const sortItems = () => {
    setItems(items.sort()); // sort mutates!
  };
}
```

### Correct: the rule accepts this

```js
function Component() {
  const [items, setItems] = useState([1, 2, 3]);

  const addItem = () => {
    setItems([...items, 4]); // New array
  };
}
```

```js
function Component() {
  const [user, setUser] = useState({name: 'Alice'});

  const updateName = () => {
    setUser({...user, name: 'Bob'}); // New object
  };
}
```

```js
function TodoList() {
  const [todos, setTodos] = useState([]);

  const addTodo = (id, text) => {
    setTodos([...todos, {id, text}]);
    // Or: setTodos(todos => [...todos, {id: Date.now(), text}])
  };

  return (
    <ul>
      {todos.map(todo => <li key={todo.id}>{todo.text}</li>)}
    </ul>
  );
}
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import reactHooks from "eslint-plugin-react-hooks";

export default [
  {
    plugins: { "react-hooks": reactHooks },
    rules: {
      "react-hooks/immutability": "error",
    },
  },
];
```

### Oxlint: `.oxlintrc.json`

```json
{
  "plugins": [
    "react"
  ],
  "rules": {
    "react/immutability": "error"
  }
}
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [React Hooks recommended](https://lintradar.dev/presets/react-hooks-recommended.md) | ESLint | `eslint-plugin-react-hooks` | 16 | 118.4M | +21.5% |
| [Next.js Core Web Vitals](https://lintradar.dev/presets/next-core-web-vitals.md) | ESLint | `eslint-config-next` | 62 | 46.8M | +48.8% |

## Related rules

- [react-hooks/globals](https://lintradar.dev/rules/react-hooks/globals.md): Validates against assignment/mutation of globals during render, part of ensuring that side effects must render outside of render
- [react-hooks/incompatible-library](https://lintradar.dev/rules/react-hooks/incompatible-library.md): Validates against usage of libraries which are incompatible with memoization (manual or automatic)
- [react-hooks/error-boundaries](https://lintradar.dev/rules/react-hooks/error-boundaries.md): Validates usage of error boundaries instead of try/catch for errors in child components
- [react-hooks/preserve-manual-memoization](https://lintradar.dev/rules/react-hooks/preserve-manual-memoization.md): Validates that existing manual memoized is preserved by the compiler. React Compiler will only compile components and hooks if its inference matches or exceeds the existing manual memoization
- [react-hooks/purity](https://lintradar.dev/rules/react-hooks/purity.md): Validates that components/hooks are pure by checking that they do not call known-impure functions
- [react-hooks/refs](https://lintradar.dev/rules/react-hooks/refs.md): Validates correct usage of refs, not reading/writing during render. See the "pitfalls" section in `useRef()` usage

---

Web page: https://lintradar.dev/rules/react-hooks/immutability. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
