Skip to content

State and props ​

React components follow the same ownership discipline as State without globals: props belong to the parent, and mutable state lives inside React (useState, useReducer), never at module scope.

In short

  • Never mutate props: treat props as read-only inputs and report updates through parent callbacks (no-param-mutation).
  • No module-level mutable state: keep component state in hooks and shared state in context or explicit stores (no-mutable-global).

Props belong to the parent ​

A component is a function, and its props are a parameter. Changing an object reached through them changes the parent's data without telling React: nothing re-renders, and the parent's copy no longer matches what it rendered (no-param-mutation):

✕ Rejectedno-param-mutation
tsx
type Todo = { title: string; done: boolean };

export function TodoItem({ todo }: { todo: Todo }) {
  return (
    <label>
      <input type="checkbox" checked={todo.done} onChange={() => { todo.done = !todo.done; }} />
      {todo.title}
    </label>
  );
}

✅ Accepted: the parent owns the todo, and is asked to change it

✓ Accepted0 diagnostics
tsx
type Todo = { readonly title: string; readonly done: boolean };

export function TodoItem({ todo, onToggle }: { todo: Todo; onToggle: () => void }) {
  return (
    <label>
      <input type="checkbox" checked={todo.done} onChange={onToggle} />
      {todo.title}
    </label>
  );
}

The same holds for state: change it with its setter, to a new value. Changing it in place (items.push(x); setItems(items)) is reported by eslint-plugin-react-hooks' immutability rule, so SAFE-TS doesn't repeat it.

State at the top of a module is shared ​

A let at the top of a file is one value for every instance of every component that uses it, for every test, and (with server rendering) for every request (no-mutable-global):

✕ Rejectedno-mutable-global
tsx
let clicks = 0;

export function Counter() {
  return <button onClick={() => { clicks += 1; }}>Clicked</button>;
}
✓ Accepted0 diagnostics
tsx
import { useState } from "react";

export function Counter() {
  const [clicks, setClicks] = useState(0);
  return <button onClick={() => setClicks((n) => n + 1)}>Clicked {clicks} times</button>;
}

State shared by several components goes in a common parent, or in a context whose value a provider creates (useState in the provider, not a module-level object).

Rules on this page: no-param-mutation, no-mutable-global

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra