Why should rendering avoid mutating props or external state?
Understand the idea, explain it in your own words and use the rubric to find your next learning gap.
The idea behind the question
React may run rendering more than once or abandon work before committing it. A render calculation should derive UI from its inputs without changing data owned elsewhere.
A component sorts its props array in place, changing the order seen by a sibling even before the user clicks anything.
// Standalone helper: run in JavaScript; apply the idea in React.
const products = [{ id: 1, price: 20 }, { id: 2, price: 10 }];
const byPrice = items => [...items].sort((a, b) => a.price - b.price);
console.log(JSON.stringify(byPrice(products).map(p => p.id)));
console.log(JSON.stringify(byPrice(products).map(p => p.id)));
console.log(JSON.stringify(products.map(p => p.id)));Show expected output
[2,1]
[2,1]
[1,2]Copy and paste to run in a separate tab. This example illustrates one concept; it is not a complete application or an automatically graded task.
A practical way to learn it
Write a pure helper that sorts products by price without mutating its input. Run it twice and verify that the original order remains unchanged.
Open React project studioThe related tool opens separately; your interview answer stays here. Written practice is self-guided, without automatic code grading.
Build an explanation you understand
- State what the concept does in plain language.
- Apply it to the example and make your assumptions explicit.
- Explain a trade-off, failure case and how you would test it.
- Distinguish your actual experience from a proposed approach.
How would you explain it?
Single-question timers reset on refresh. Full mock sessions can resume from this browser.
Strengthen your explanation
Compare reasoning, not identical wording. Reference answers describe a possible approach; replace hypothetical statements with truthful details from your own work.
One way to answer
I would keep rendering pure and create a new array before sorting or transforming shared input. An event handler can initiate an action, and an effect can synchronize a committed component with an external system when that is necessary. A ref can hold mutable information that does not drive rendering, but changing it is not a substitute for state updates. I would test with frozen inputs and development checks to catch accidental mutation. Repeated rendering should compute the same result from the same inputs without producing extra writes or subscriptions.
What a strong explanation should cover
Tick only the points your own answer addresses. This is self-assessment, not AI scoring.
0 / 3 points self-reportedWatch out for
- Disabling development checks to hide duplicate side effects.
Now go one level deeper
- Why can writing a ref during render still be unsafe?
- Would useMemo make an impure calculation safe?
Further reading
Further-reading links support learning; the reference is Sukoaa-authored, not an official employer answer key.