The basic memory of a component. React stores the value, returns a fixed snapshot on each render, and re-renders when you call the setter with a new value.
const [count, setCount] = useState(0);
setCount(count + 1); // ask for the next render
React · the core model
Five hooks show how React works. It calls your component again, compares what comes back with what it had, and writes only the difference to the page. Everything else stays where it is.
A component takes props and state and returns a description of the UI. It does nothing else while rendering, so React can run it again whenever it needs to.
State is replaced, not changed in place. A new object tells React something changed. The same object tells it nothing did.
A state change starts a render in memory. Only afterwards does React commit the differences to the DOM and let the browser paint.
State
A component is a function that React calls on every render. Each call starts with fresh local variables, so a value you assign inside the function is gone on the next call.
React keeps the value outside your function, hands it back on every call, and gives you a setter. Calling the setter tells React to run the function again with the new value.
let count = 0local variable, reset by every calluseState(0)kept by React, changed by the setterInside one render, count never changes. setCount(count + 1) asks for the next render, and that render receives the new value.
count = count + 1 edits a variable that disappears. Only the setter lets React know the screen is out of date.
Render cycle
Every update follows the same six steps. The first three happen in memory. React touches the DOM only at step 4, and only where the new tree differs from the old one.
A setter or dispatch is called, or a parent renders. React marks the component as needing a new render.
React calls your component. Same props and state in, same JSX out, with no side effects. The DOM is not touched.
React compares the new element tree with the previous one and keeps a list of what is different.
React writes only that list to the real DOM. Refs to DOM nodes are attached here.
The browser draws the pixels that changed.
After paint, each useEffect whose dependencies changed runs. The old cleanup runs first. A setter called here starts the cycle again.
Try it
Counter owns the state, so only Counter and its children run again. Header and Footer are not called, and the DOM receives a single text change.
Press the button. The cycle plays slowly, one step at a time.
Label and Button run again because their parent did. React.memo could skip them, and the DOM would still receive only one text change.
Pure function · Immutability
React can re-run, compare and skip work only because render has no side effects and state values are never mutated.
Same props and state in, same JSX out. No fetching, no timers, no writing to variables outside the component. React may call your component more than once, and in development StrictMode does it on purpose to expose impure code. Side effects belong in event handlers and useEffect.
// pure: safe to run twice
function Total({ price, qty }) {
return <p>{price * qty}</p>;
}
// impure: changes the outside world
let renders = 0;
function Total({ price }) {
renders++;
return <p>{price * Math.random()}</p>;
}
React decides whether state changed by comparing references with Object.is. The same comparison drives the dependency arrays of useEffect and useMemo.
cart.push("tea");
setCart(cart);
Object.is → true. React skips the render and the screen stays stale until something else renders.
setCart([...cart, "tea"]);
// a new array, a new reference
Object.is → false. React renders, diffs and commits.
Hooks
Four hooks let a function remember. One lets it talk to the world outside React.
The basic memory of a component. React stores the value, returns a fixed snapshot on each render, and re-renders when you call the setter with a new value.
const [count, setCount] = useState(0);
setCount(count + 1); // ask for the next render
An effect keeps something outside React (the tab title, a timer, a subscription) in step with state. The dependency array names the state it synchronizes with. If those values are unchanged, the effect is skipped.
useEffect(() => {
document.title = `Count: ${count}`;
}, [count]); // synchronizes with count
A box with a .current field. It is the same box on every render, and writing to it does not trigger one. Use it for DOM nodes, timer ids and values the screen does not show.
const clicks = useRef(0);
function onClick() {
clicks.current += 1; // kept, no render
}
Caches a calculation between renders and reuses it while the dependencies compare equal. Because of Object.is, a new array or object recalculates it. It is an optimization, and React may drop the cache.
const double = useMemo(
() => slowDouble(count),
[count]
); // recomputed only if count changes
When many fields change together, every change goes through one pure function: (state, action) => newState. The reducer returns a new object, so immutability is built into the pattern, and every transition has a name.
function reducer(state, action) {
switch (action.type) {
case "add":
return { ...state, items: [...state.items, action.item] };
case "clear":
return { ...state, items: [] };
}
}
const [cart, dispatch] = useReducer(reducer, { items: [] });
dispatch({ type: "add", item: "tea" });
Across renders
One component with count, a cart, a clicks ref, a memoized double and an effect on [count].
The whole model
It re-runs a pure function, compares two descriptions of the UI, and writes the difference. Hooks decide what the function remembers and what it connects to.
State and reducer hold what the screen shows. Ref and memo hold what survives between renders without being shown.
Effects keep the outside world in step with state.