Consider a React functional component receiving props. What will happen if the component tries to change a prop value directly inside its body?
function Child({ count }) { count = count + 1; return <p>{count}</p>; }
Think about how JavaScript variables and React props work inside a function.
Props are read-only, but assigning a new value to the prop variable inside the component only changes the local copy. It does not affect the parent or cause errors. The component renders with the new local value.
Look at this React component. What will it display after mounting?
function Example({ message }) { React.useEffect(() => { message = 'Changed'; }, []); return <p>{message}</p>; }
Remember that props are read-only and reassigning them inside effects does not update state or cause re-render.
Reassigning the prop variable inside useEffect only changes the local variable. It does not update the component's state or cause a re-render. So the original prop value is rendered.
You want to ensure a child component does not accidentally mutate props. Which code snippet correctly enforces this?
Think about how to make an object immutable in JavaScript.
Using Object.freeze(props) prevents any changes to the props object. Attempting to assign a new value to a prop will fail silently or throw in strict mode. Other options either mutate props directly or create new variables.
Examine this React component. Why does changing props.data.name inside the component cause bugs?
function Profile(props) { props.data.name = 'New Name'; return <p>{props.data.name}</p>; }
Consider how JavaScript objects and React props work together.
Props are immutable only at the top level. Nested objects inside props can be changed, which mutates the original object and causes bugs because the parent component's data changes unexpectedly.
Choose the best explanation for why React components must treat props as read-only data.
Think about how React decides when to update components.
React uses props immutability to detect changes via shallow comparison. If props are mutated directly, React may not detect changes properly, leading to bugs and inefficient rendering.