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
setStateinside 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
nullto 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 />)fromreact-dom/client, which mounts the app (replacingReactDOM.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";34type Props = { items: string[] };56class List extends Component<Props> {7 render() {8 // Pure: only reads props/state and returns a description9 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}1920// Function component: the body is the render21function ListFn({ items }: Props) {22 if (items.length === 0) return null;23 return <ul>{items.map((i) => <li key={i}>{i}</li>)}</ul>;24}2526// 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.