ReactEasy

What is the render method?

render() is the one required method of a class component: it reads this.props and this.state and returns what to display (JSX, a string, a number, null, an array or a portal). It must be pure, meaning no side effects and no state updates. In function components, the function body itself plays the role of render.

Rules for render

  • Pure: same props and state give the same output. No fetching, subscriptions, timers or DOM mutation.
  • No setState inside it; that causes an infinite loop.
  • Can be called many times and at unpredictable moments (concurrent rendering, Strict Mode double rendering in development), so it must be cheap and side-effect free.
  • Return null to render nothing.

What it is not

  • It does not touch the DOM itself. It returns a description; React decides when and how to update the DOM in the commit phase.
  • Not to be confused with root.render(<App />) from react-dom/client, which mounts the app (replacing ReactDOM.render, removed in React 19).

Where side effects go

componentDidMount and componentDidUpdate in classes, or useEffect and event handlers in function components.

Performance

Heavy computation in render runs on every update. Memoize with useMemo (or let the React Compiler do it), or move work out of the component.

Code example

tsx
1import { Component } from "react";
2import { createRoot } from "react-dom/client";
3
4type Props = { items: string[] };
5
6class List extends Component<Props> {
7 render() {
8 // Pure: only reads props/state and returns a description
9 if (this.props.items.length === 0) return null;
10 return (
11 <ul>
12 {this.props.items.map((item) => (
13 <li key={item}>{item}</li>
14 ))}
15 </ul>
16 );
17 }
18}
19
20// Function component: the body is the render
21function ListFn({ items }: Props) {
22 if (items.length === 0) return null;
23 return <ul>{items.map((i) => <li key={i}>{i}</li>)}</ul>;
24}
25
26// Mounting the app (ReactDOM.render was removed in React 19)
27const container = document.getElementById("root");
28if (container) createRoot(container).render(<><List items={["a"]} /><ListFn items={["b"]} /></>);

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.

Open in study app