Props Read-Only Rule and Pure Components
What you'll learn: How the read-only props rule helps React components behave predictably and avoid bugs.
The Core Idea
React components should act like pure functions: given the same props, they always return the same JSX, without causing side effects during render. This is called a "pure component."
Think of it like a recipe. If you follow the exact same recipe with the exact same ingredients, you should get the exact same dish every time. The recipe doesn't change the ingredients themselves—it just uses them to create something new.
Why Props Must Be Read-Only
When React enforces that props cannot be modified, it guarantees predictability:
function Greeting(props) {
// ✅ Pure: same props in, same JSX out
return <h1>Hello, {props.name}!</h1>;
}
function BadGreeting(props) {
// ❌ Impure: modifying props (causes error)
props.name = props.name.toUpperCase();
return <h1>Hello, {props.name}!</h1>;
}
A pure component has no side effects during render—it doesn't modify external variables, make API calls, or change its props. It simply transforms props into JSX.
The Benefits
- Predictability: Same input → same output, always
- Testability: Easy to test because behavior is consistent
- Performance: React can safely skip re-renders when props haven't changed
- Debugging: No hidden state changes to track down
If you need to transform prop data, create new variables instead of mutating props:
function FormattedName(props) {
const uppercaseName = props.name.toUpperCase(); // ✅ New variable
return <h1>{uppercaseName}</h1>;
}
Key Takeaway: Keep components pure by treating props as read-only and avoiding side effects during render—same props in, same JSX out, every single time.