The wrong room keeps receiving updates
A ChatRoom subscribes to a room in an effect. After switching from room A to B, the UI still receives messages for A. Walk through the expected subscription lifecycle and explain how you would check that the fix works in development.
Model Answer & Explanation
Make the effect depend on the room ID and any other reactive subscription inputs. Return cleanup that unsubscribes the exact subscription created for that run. On a room change React cleans up the previous setup before running the new one, and cleans up on unmount. At the root, development Strict Mode can run an extra setup/cleanup cycle, so verify there is exactly one active subscription after mounting and switching.
What a Strong Answer Covers
- Use current dependencies and clean up the subscription that was actually created.
- Test lifecycle behavior after setup, room changes and unmount.