0
0
Reactframework~20 mins

Props as read-only data in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Props Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a child component tries to modify props directly?

Consider a React functional component receiving props. What will happen if the component tries to change a prop value directly inside its body?

React
function Child({ count }) {
  count = count + 1;
  return <p>{count}</p>;
}
AThe component will render with the incremented count, but the parent prop remains unchanged.
BReact will throw a runtime error because props are immutable.
CThe parent component's state will update automatically to reflect the change.
DThe component will ignore the change and render the original prop value.
Attempts:
2 left
💡 Hint

Think about how JavaScript variables and React props work inside a function.

state_output
intermediate
2:00remaining
What is the output when trying to mutate props inside useEffect?

Look at this React component. What will it display after mounting?

React
function Example({ message }) {
  React.useEffect(() => {
    message = 'Changed';
  }, []);
  return <p>{message}</p>;
}
AAn error is thrown because props cannot be changed inside useEffect.
B<p>Changed</p>
C<p>Original message passed as prop (unchanged)</p>
DNothing is rendered because the component crashes.
Attempts:
2 left
💡 Hint

Remember that props are read-only and reassigning them inside effects does not update state or cause re-render.

📝 Syntax
advanced
2:00remaining
Which option correctly prevents prop mutation in a child component?

You want to ensure a child component does not accidentally mutate props. Which code snippet correctly enforces this?

A
function Child(props) {
  const newProps = { ...props, value: 10 };
  return &lt;p&gt;{newProps.value}&lt;/p&gt;;
}
B
function Child(props) {
  props.value = 10;
  return &lt;p&gt;{props.value}&lt;/p&gt;;
}
C
function Child({ value }) {
  value = 10;
  return &lt;p&gt;{value}&lt;/p&gt;;
}
D
function Child(props) {
  Object.freeze(props);
  props.value = 10;
  return &lt;p&gt;{props.value}&lt;/p&gt;;
}
Attempts:
2 left
💡 Hint

Think about how to make an object immutable in JavaScript.

🔧 Debug
advanced
2:00remaining
Why does this code cause unexpected behavior when modifying props?

Examine this React component. Why does changing props.data.name inside the component cause bugs?

React
function Profile(props) {
  props.data.name = 'New Name';
  return <p>{props.data.name}</p>;
}
AProps are shallowly immutable; mutating nested objects causes side effects outside the component.
BReact automatically clones props, so changes inside the component do not affect the parent.
CThis code throws a TypeError because nested props cannot be changed.
DThe component will not render because props are frozen by default.
Attempts:
2 left
💡 Hint

Consider how JavaScript objects and React props work together.

🧠 Conceptual
expert
2:00remaining
Why is treating props as read-only important in React?

Choose the best explanation for why React components must treat props as read-only data.

ABecause props are only used for styling and cannot hold dynamic data.
BBecause React relies on props immutability to detect changes and optimize rendering efficiently.
CBecause modifying props directly causes React to automatically reset the component state.
DBecause props are stored in a special read-only memory area by the browser.
Attempts:
2 left
💡 Hint

Think about how React decides when to update components.