A draft leaks into a different conversation
A ChatPane holds its input draft in local state. Switching from Alice to Bob changes the recipient prop, but Alice’s unsent draft remains in the input. Explain why. How would you implement a fresh draft on switch? What if the product instead wants to preserve a separate draft for each recipient?
Model Answer & Explanation
The same component type remains at the same tree position with the same identity, so changing a prop does not reset its local state. Use key={recipientId} on ChatPane to intentionally remount and reset it. For distinct retained drafts, lift draft state to a parent map keyed by recipient ID or store it elsewhere with the same identity.
What a Strong Answer Covers
- Explain how React preserves state across prop changes.
- Distinguish resetting a subtree from retaining state by recipient.