Code Review: A Junior's React Change
A junior engineer on your team opened this pull request. It works in the happy path. Review it as you would in the real round: list the issues you would raise, prioritised by impact (correctness, security, performance, accessibility, maintainability); say how you would deliver the feedback so the author learns rather than just complies; outline the refactors you would suggest; name the tests you would add or request; and describe how you would follow up after the review.
export default function UserList(props: any) {
const [users, setUsers] = useState([]);
const [q, setQ] = useState("");
fetch(`/api/users?team=${props.team}`)
.then((r) => r.json())
.then((data) => setUsers(data));
function handleDelete(id) {
users.splice(users.findIndex((u) => u.id == id), 1);
setUsers(users);
fetch(`/api/users/${id}`, { method: "DELETE" });
}
return (
<div>
<input onChange={(e) => setQ(e.target.value)} />
{users
.filter((u) => u.name.includes(q))
.map((u, i) => (
<div key={i} onClick={() => handleDelete(u.id)} style={{ cursor: "pointer" }}>
{u.name} - {u.email}
<span dangerouslySetInnerHTML={{ __html: u.bio }} />
</div>
))}
</div>
);
}Assume you may ask clarifying questions about requirements. The interviewers afterwards ask about your experience mentoring.
Hints
Hint 1
Order by blast radius: a request on every render and an injection vector come before naming and typing.
Hint 2
Separate what is wrong from why it matters and how to fix it; the second part is what the author keeps.
Hint 3
Ask before telling where the intent is unclear (is bio trusted HTML? is a click really meant to delete?); questions model the review habit you want them to pick up.
Solution
Show the approach
Issues, in priority order
- Fetch in render — runs on every render and each response triggers another render: an infinite loop of requests. Move it into
useEffectkeyed onprops.team, with anAbortControllerin the cleanup so a stale team's response cannot overwrite a newer one. dangerouslySetInnerHTMLonbio— stored XSS if bios are user-authored. Render as text, or sanitise server-side and justify in a comment why HTML is needed.- Mutating state —
spliceon the state array thensetUsers(users)passes the same reference, so React may not re-render, and it loses the user before the server confirms. UsesetUsers(prev => prev.filter(...)), and either do it after theDELETEsucceeds or revert on failure; also handle the fetch's errors, which are currently swallowed. - Click to delete on a
div— not keyboard reachable, no role, no confirmation, and a mis-click destroys data. A<button>with a clear label and a confirm step. - Index keys — deleting an item shifts every index, so React reuses the wrong rows. Key by
u.id. - No loading or error state, and
==where===is meant,props: anylosing the type contract, an unlabelled input, a case-sensitive filter, and unmemoised filtering (fine now, say so, and when it would matter).
Delivering it
Open with what is good (clear structure, small component). Group comments by severity and mark which block the merge. For each blocker explain the failure a user would see, not just the rule. Ask questions where intent matters ("Is bio trusted HTML? If not, what should it render as?"). Offer to pair on the effect and the delete flow rather than rewriting them in the review.
Refactor shape
A useUsers(team) hook owning fetch, loading and error state; a UserRow component with a delete button; a typed User and Props; the filter in a useMemo with the query lower-cased once.
Tests
Unit: renders loading, then rows; filter is case-insensitive; delete removes the right row by id and calls the endpoint once; delete failure restores the row. Integration: switching team cancels the old request. Accessibility: the delete control is reachable by keyboard and has a name.
Follow-up
A short pairing session on effects and immutability, a team convention written down (no dangerouslySetInnerHTML without a sanitiser; keys from ids), lint rules that catch the mechanical ones (react-hooks/exhaustive-deps, eqeqeq, no-array-index-key), and a check-in on their next review to see what stuck.
Assistant
Sign in to use the assistant — ask what the problem means, get a hint when you're stuck, or have your current work reviewed. Conversations save to your account.