What if changing data inside child components is the hidden cause of your app's bugs?
Why Props as read-only data in React? - Purpose & Use Cases
Imagine you have a parent component passing data to many child components, and you try to change that data inside the child directly.
It's like lending a friend your favorite book and expecting them to rewrite it -- chaos and confusion follow.
Changing data inside child components causes bugs and unpredictable app behavior.
It's hard to track where changes happen, leading to broken UI and wasted time fixing errors.
React treats props as read-only, so child components only read data without changing it.
This keeps data flow clear and predictable, making apps easier to understand and debug.
function Child(props) { props.name = 'New Name'; return <div>{props.name}</div>; }function Child({ name }) { return <div>{name}</div>; }This approach enables reliable, one-way data flow that keeps your app stable and easy to maintain.
Think of a family photo album passed around: everyone can look at the pictures (props), but no one can change the original photos inside the album.
Props are like read-only gifts from parent to child components.
Trying to change props inside children causes bugs and confusion.
Using props as read-only keeps data flow simple and apps reliable.
Practice
props are read-only?Solution
Step 1: Understand the role of props
Props are data passed from a parent component to a child component in React.Step 2: Recognize props immutability
Props are read-only, meaning the child component cannot change them directly.Final Answer:
You cannot changepropsinside the child component. -> Option AQuick Check:
Props are read-only = D [OK]
- Trying to modify props directly inside child component
- Confusing props with state
- Thinking props are for styling only
title inside a functional component?Solution
Step 1: Identify functional component syntax
In functional components, props are passed as a parameter namedprops.Step 2: Access prop correctly
You access a prop byprops.title. Usingthis.propsis for class components.Final Answer:
const title = props.title; -> Option DQuick Check:
Functional component props access = B [OK]
- Using this.props in functional components
- Confusing state with props
- Trying to use undefined variables like getProps
function Greeting(props) {
props.name = "Alice";
return <h1>Hello, {props.name}!</h1>;
}
// Usage: <Greeting name="Bob" />Solution
Step 1: Understand props mutability
Although props are read-only and mutating them is an anti-pattern, JavaScript allows changing the object property within the component function.Step 2: Determine rendered output
The assignmentprops.name = "Alice"mutates the value before the return, so the output is "Hello, Alice!".Final Answer:
Hello, Alice! -> Option BQuick Check:
Props mutation affects render output = B [OK]
- Thinking props mutation doesn't affect output
- Expecting an error from assignment
- Confusing props with state
function Counter(props) {
props.count = props.count + 1;
return <div>Count: {props.count}</div>;
}Solution
Step 1: Check for props mutation
The lineprops.count = props.count + 1;tries to change props directly, which is not allowed.Step 2: Understand correct data handling
Props are read-only; to change data, use state or callbacks, not direct mutation.Final Answer:
Props are being mutated directly, which is not allowed. -> Option AQuick Check:
Direct props mutation = error = A [OK]
- Trying to mutate props directly
- Confusing props with state usage
- Ignoring React's immutability rules
props are read-only?Solution
Step 1: Understand props immutability
Props cannot be changed inside the component; they are read-only.Step 2: Use state for data that changes
Copy the prop value to a state variable, then update the state to reflect changes.Final Answer:
Copy props.name to a state variable and update the state to change the name. -> Option CQuick Check:
Use state to update data, not props = C [OK]
- Trying to change props directly
- Using global variables instead of state
- Passing props from child to parent (not possible)
Ready to prove it?
Pass the test and this pattern moves unknown
