Components are Pure Functions of Props
What you'll learn: Why React components should behave like mathematical functions — same input always produces the same output.
The Core Principle
A pure function is predictable: given the same inputs, it always returns the same result. React components should follow this rule. When you pass the same props to a component, it should always render the same JSX output.
Think of a component like a recipe. If you use the exact same ingredients (props) and follow the same steps, you should get the exact same dish every time. No surprises, no randomness.
What Makes a Component Pure?
A pure component:
- Only uses its props to determine what to render
- Never modifies external variables during rendering
- Doesn't rely on changing global state like
Math.random()orDate.now()during render
Example of Purity
function Greeting(props) {
return <h1>Hello, {props.name}!</h1>;
}
This is pure. Call <Greeting name="Alice" /> a hundred times, you'll get "Hello, Alice!" every single time.
What Breaks Purity?
Avoid this during rendering:
function BadCounter() {
let count = 0;
count++; // Side effect during render!
return <p>Count: {count}</p>;
}
This component modifies a variable during rendering — that's a side effect. Pure components don't mutate things while rendering.
Why Does This Matter?
Purity makes your components predictable and testable. React can safely re-render components multiple times knowing the output won't change unexpectedly.
Key Takeaway: A React component should be a pure function — same props in, same JSX out, with no side effects during rendering.