Solution: Refactored Comment Viewer App
Stabilize identities in App, make React.memo effective in CommentList, fix selector behavior in StatsPanel, and memoize heavy analytics so the Comment Viewer becomes responsive under React 19.
Refactored comment viewer app
The value of this exercise lies in seeing how small fixes combine to form a clearer mental model of React 19’s rendering and performance behavior. This solution walks through the final refactor, tying each change back to the symptoms observed in the broken version.
Explanation
Here’s an explanation of the code above:
In the
App.jsfile:Lines 6–11: Define
textand a fixedcommentsarray;commentsnever changes, which makes it safe and memo-friendly.Line 13: The
useTransitionallows React to mark text updates as non-urgent, keeping the UI responsive during expensive work.Line 15: The
filteris wrapped inuseMemo, so{ query: text }is recreated only whentextchanges instead of every render.Lines 17–21: The
filteredCommentsis memoized; the filtered array is stable unlesstextorcommentsactually change.Lines ...