Props Are Immutable in React
What you'll learn: Why props are read-only and cannot be changed by the component that receives them.
In React, props work like a one-way delivery system. When a parent component passes props down to a child, those values are immutable—meaning the child component cannot modify them. This is a fundamental rule that keeps your component behavior predictable.
Think of props like ingredients delivered to a restaurant kitchen. The kitchen can use those ingredients to prepare a dish, but it cannot change what was delivered. If the kitchen needs different ingredients, it must ask the supplier (the parent component) to send new ones.
Why Props Must Be Read-Only
React enforces immutability for props because:
- Predictability: If children could modify props, the parent wouldn't know what values it passed
- Unidirectional data flow: Data flows down from parent to child, never sideways or upward
- Pure functions: Components should behave like pure functions—same inputs (props) always produce the same output (UI)
What Happens If You Try
If you attempt to modify a prop directly, React will throw an error in strict mode:
function Greeting(props) {
// ❌ This will cause an error!
props.name = "Changed Name";
return <h1>Hello, {props.name}</h1>;
}
The props object is frozen—attempting to change it violates React's design principles.
The Right Way
If a component needs to work with a changing value, it should use state (which you'll learn later) or ask the parent to pass a different prop value on the next render.
Key Takeaway: Props are read-only values passed from parent to child. The receiving component can use them but never modify them—this maintains React's predictable, one-way data flow.