Concept
Rendering
When and why components render, and what triggers a re-render.
Rendering explained
In React, rendering means calling your component functions to work out what the UI should look like. A component re-renders when its state changes, when its parent re-renders, or when a context it reads changes. Rendering itself does not touch the page: React compares the new result with the previous one and then commits only the differences to the DOM.
Render and commit
- Trigger: a state update (
setState), a parent rendering, or a context value changing. - Render: React calls the components and builds a new tree of elements. This must be pure: same props and state, same output, no side effects.
- Commit: React applies the minimal DOM changes, then runs effects.
Re-rendering is normal and usually cheap. It only becomes a problem when a component does heavy work during render or when a large tree re-renders on every keystroke.
Keeping renders fast
- Keep state close to where it is used, so updates re-render a small part of the tree.
React.memoskips re-rendering a component whose props are unchanged; it pairs withuseMemoanduseCallbackto keep props stable.- Move expensive calculations out of render, or cache them with
useMemo. - Use the Profiler in React DevTools to find what actually renders and why, before optimising.
Common mistakes
- Side effects during render, such as fetching data or changing a variable outside the component.
- Creating new objects or functions as props every render, which defeats
React.memo. - Setting state during render without a condition, causing an infinite loop.
- Optimising everything with memo by default instead of measuring first.
What interviewers ask about React rendering
- "When does a component re-render?" State change, parent render, or context change.
- "Does a re-render always update the DOM?" No, only if the output differs.
- "How would you stop a list re-rendering on every keystroke?" Move the input's state down, memoise the list, keep props stable.
Learn firstState
Rendering
What should you learn before Rendering?
- Components and compositionBuilding a UI from small reusable pieces and combining them.
- PropsInputs passed from a parent component to a child.
- StateState changes are what trigger renders.
What to learn after Rendering
- Concurrent renderingReact preparing several UI versions at once and interrupting low-priority work.
- MemoizationCaching a computed result or component so it is not recomputed when inputs have not changed.
- Reconciliation and keysHow React compares the old and new tree, and why list items need stable keys.
- Server-side rendering and hydrationRendering HTML on the server, then attaching interactivity in the browser.
Practise Rendering
- React Interview Questions for Freshers4 questions on Rendering
- Intermediate React Interview Questions3 questions on Rendering
- Advanced React Interview Questions3 questions on Rendering
- Top 20 JavaScript interview questions with answers: core JS, Node.js and React2 questions on Rendering