Home / Learn React / The Rules of React
The Rules of React
React works best when components are pure and hooks are called in a fixed way. These rules also let the compiler and StrictMode help you.
Theory in plain words
What it is
The Rules of React are a small set of conventions: components and hooks must be pure (same input, same output), props and state are read-only, and hooks are called unconditionally at the top level.
Why we use it
React may render a component more than once, pause it or skip it. That only works safely if rendering has no hidden side effects. Following the rules makes bugs rare and lets tools optimize your code.
How it works
During render, calculate and return JSX. Do side effects (network, timers, DOM changes) in event handlers or useEffect. Update state by creating new objects and arrays.
Where it is used
- Every React codebase, and a requirement for React Compiler
Mutation bug
Click the wrong button: nothing changes on screen. Click the right button: you see the new items and also the ones pushed earlier, because the data had changed but React did not re-render.
Pure render
In StrictMode, React renders twice on purpose, so the impure component skips a number. That is how StrictMode exposes the problem.
- Do not use
Math.random()orDate.now()in the render body for visible output. Use state or an effect. - Never call hooks inside
if, loops, nested functions or after an earlyreturn. - Install
eslint-plugin-react-hooks. It catches most rule breaks automatically.
Quick quiz
1. What should a component do while rendering?
Same input, same output.
2. Where can hooks be called?
Never conditionally.
3. Why does list.push(x); setList(list) fail?
Create a new array.
Common questions
What should a component do while rendering?
Be pure and only calculate JSX. Same input, same output.
Where can hooks be called?
At the top level of components and custom hooks. Never conditionally.
Why does list.push(x); setList(list) fail?
The array reference did not change. Create a new array.