Full answer
Props are the inputs a parent gives to a child. The rule in React docs is that all components must act like pure functions with respect to their props. A pure function does not modify its inputs, so a component should never write to props.name or mutate an object or array it received.
Reasons. First, predictability: the parent owns the data. If a child could change it, two components would hold different views of the same value, and the next parent render would overwrite the child's change. Second, React relies on comparing values by reference to skip work (memo, effect dependencies, useMemo). Mutating an object in place leaves its reference unchanged, so React cannot see the change and the UI goes stale. Third, concurrent rendering may render a component multiple times or discard a render, so side effects on shared data lead to bugs.
What happens in practice. In development, React freezes the props object (Object.freeze), so props.title = 'x' throws a TypeError in strict mode code, which includes ES modules and classes. The freeze is shallow: it protects the props object itself, not the objects inside it. A child can still mutate props.user.name without an error, and that is a bug that React cannot catch for you. In production the freeze is not applied, so the mistake can silently work and then fail in confusing ways.
What to do instead:
- To change a value owned by the parent, call a function passed as a prop (
onRename(newName)). - To keep an editable draft, copy the prop into state once (
useState(props.initialName)), name the propinitialNameto make the intent clear, and decide how to sync it (key reset). - To transform a value, compute a new one during render without changing the original (
[...props.items].sort(...), since sort mutates).
The example shows the frozen object and a safe derived copy.
import { renderToStaticMarkup } from 'react-dom/server';
function Course(props) {
console.log('props object is frozen: ' + Object.isFrozen(props));
try {
// ES modules run in strict mode, so assigning to a frozen object throws.
props.title = 'Changed';
} catch (error) {
console.log('assigning to props threw: ' + error.constructor.name);
}
const sorted = [...props.lessons].sort();
return (
<p>
{props.title}: {sorted.join(', ')}
</p>
);
}
const lessons = ['State', 'Props', 'Effects'];
console.log(renderToStaticMarkup(<Course title="React" lessons={lessons} />));
console.log('original array unchanged: ' + lessons.join(', '));The first two lines show the freeze in the development build of React, which is what this script ran. A production build does not freeze the object. The array copy keeps the original order. Output of the run: props object is frozen: true assigning to props threw: TypeError <p>React: Effects, Props, State</p> original array unchanged: State, Props, Effects
Key points
- Components should behave like pure functions of their props and never modify them.
- Mutating props breaks the parent's ownership and defeats reference comparisons.
- In development React freezes the props object, so assignment throws in strict mode code. The freeze is shallow.
- Use callback props to request changes, or copy into state for a local draft.
- Copy before sorting or reversing prop arrays, because those methods mutate.
Common mistakes
- Assigning to props inside a component.
- Calling sort or reverse on an array received as a prop.
- Mutating a nested object from props because the freeze is shallow.
- Copying a prop into state and expecting it to follow later prop changes.
- Relying on the development error as the only protection.
Follow-ups
- How would you keep a form draft that starts from a prop value and resets when the prop changes?
- Why does the freeze not apply to nested objects?
- How do TypeScript readonly types help here?
Tips
- Describe components as pure functions of props.
- Mention that the freeze is dev-only and shallow.
- Give the callback pattern as the correct way to change parent data.