The render() Method: Class Components' Return
You'll learn why render() is the heart of every class component and what rules it must follow.
The Class Component's Return Value
In function components, you simply return JSX directly from the function body. In class components, you must define a render() method that returns your JSX. This method is what React calls to figure out what should appear on screen.
Think of render() as the "what should I display?" question that React asks your component. Your answer comes in the form of JSX returned from this method.
class Greeting extends React.Component {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}
Notice the structure: render() is a method inside the class, and it contains a return statement with your JSX.
The Purity Rule
Just like function components themselves, render() must be pure. This means:
- No side effects: Don't modify state, make network requests, or change variables outside the method
- Same props = same output: Given identical props,
render()should always return the same JSX - No mutations: Don't modify
this.propsor other data
Why? React may call render() multiple times, and it needs to trust that each call is predictable and safe. Impure render methods lead to unpredictable UI and subtle bugs.
// ❌ Impure - modifies external state
render() {
document.title = "Bad!"; // Side effect!
return <div>Content</div>;
}
// ✅ Pure - just returns JSX based on props
render() {
return <div>Hello {this.props.name}</div>;
}
Key Takeaway: The render() method is a class component's required return mechanism—it must be pure and only concerned with transforming props into JSX.