React Rendering and Re-renders
Interviewers love asking “when does a React component re-render?” because the answer reveals whether you understand React’s mental model or just its syntax. Here’s the precise version. React’s own guide to render and commit is the best primer on React rendering and re-renders.
1. What a “render” is
A render is React calling your component function to get a new description of the UI (React elements). Rendering is not the same as updating the DOM. After rendering, React reconciles the new element tree against the previous one and only commits the differences to the DOM.
render (call component) -> reconcile (diff) -> commit (touch DOM, run effects)
A component can render many times without the DOM changing at all.
2. What triggers a re-render
- State change: calling a
useStatesetter oruseReducerdispatch with a new value. If the new value is identical (Object.is) to the current one, React bails out. - Parent re-render: by default, when a component renders, all of its children render too, whether or not their props changed.
- Context change: every component that calls
useContext(SomeContext)re-renders when the provider’svaluechanges.
Notice what is not on the list: “props changed”. Props changing is a consequence of a parent rendering, not an independent trigger.
3. Why mutating state does nothing
const [items, setItems] = useState([]);
function add(item) {
items.push(item); // mutates the same array
setItems(items); // same reference -> Object.is says equal -> no re-render
}
function addCorrectly(item) {
setItems(prev => [...prev, item]); // new reference -> re-render
}
4. Skipping renders with React.memo
React.memo(Component) makes a child skip rendering when its props are shallowly equal to the previous props. Shallow equality compares each prop with Object.is, so a new object, array, or function created during the parent’s render counts as a change.
const Row = React.memo(function Row({ item, onSelect }) { ... });
function List({ items }) {
// New function every render -> Row re-renders anyway
return items.map(item => <Row key={item.id} item={item} onSelect={() => select(item.id)} />);
}
5. useMemo and useCallback
useMemo(fn, deps)caches a computed value between renders until a dependency changes.useCallback(fn, deps)caches a function reference. It is equivalent touseMemo(() => fn, deps).
They matter mostly when the value is passed to a memoized child or used as an effect dependency. Wrapping everything adds overhead and noise without benefit.
6. Cheaper than memoizing: restructure
- Move state down into the component that actually needs it so fewer components re-render.
- Lift content up: pass expensive subtrees as
children. Elements created by the parent keep the same reference when only the wrapper’s state changes.
function Hoverable({ children }) {
const [hovered, setHovered] = useState(false);
return <div onMouseEnter={() => setHovered(true)}>{children}</div>;
}
// <Hoverable><ExpensiveTree /></Hoverable> -> ExpensiveTree does not re-render on hover
Key Takeaways on React Rendering and Re-renders
| Question | Answer |
|---|---|
| Does a re-render always update the DOM? | No, only the diff is committed |
| Do children re-render when the parent does? | Yes, unless memoized with equal props |
| Does setting the same state value re-render? | No, React bails out via Object.is |
| First fix for wasted renders? | Restructure (move state down, pass children) before memoizing |
