Reviewing a Junior's React Component: Feedback and Refactors
BehavioralModel answerLast updated
Walk through how you would review a junior engineer's messy React/TypeScript pull request: rank the issues, give feedback that teaches, propose refactors, request tests and plan follow-up.
Scenario
A junior teammate has opened a pull request for a frontend feature. It is a React/TypeScript component that loads data from an API and keeps it in local state. The feature works when you click through it, but the code is hard to read, hard to change and fragile. You are the reviewer.
You may ask clarifying questions about requirements, deadlines and team conventions before you answer.
What to cover
- Findings: Which problems would you raise, and in what order? Rank them by impact across correctness, security, performance, accessibility and maintainability. Say which would block the merge.
- Delivery: How would you word and structure your comments so the author learns and stays motivated? When would you ask a question, and when would you state the fix outright?
- Refactors: Which concrete changes would you suggest for component structure, state management, separation of concerns, naming, typing and error handling?
- Tests: What tests would you ask for (unit, integration, end-to-end), and which behaviors should they pin down?
- Follow-up: What would you do after the review to help the author improve and to stop the same issues from recurring?
A strong answer shows both technical judgment and the ability to coach.
Hints
Hint 1
Start with a few clarifying questions (urgency, team conventions, what the author understood the requirements to be) because they decide what blocks the merge and what becomes a follow-up.
Hint 2
Sort findings into tiers: security and correctness first (secrets in the client, unsanitized HTML, missing effect dependencies, no error or loading handling, race conditions), then accessibility, then maintainability, and leave style to the linter.
Hint 3
For feedback, label every comment as blocking, should-fix or nit, critique the code rather than the person, ask questions where the reasoning is the lesson, and move to a live pairing session if comments pile up.
Hint 4
Tie each test to a bug you found: a regression test for the stale or racing response, an error-state test, an XSS test, and a role-based query that proves the control is keyboard accessible.
Model answer
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Model answer
A good answer covers two things at once: technical judgment (finding real problems and ranking them) and mentoring (making the author better, not just the diff). The failure modes are a flat list of 30 nitpicks, or being so gentle that a security bug gets merged.
1. Clarify first
- Urgency: is this blocking a release, or is there room to iterate?
- Conventions: is there a style guide, lint setup, or standard data-fetching approach (React Query, SWR, Redux Toolkit)?
- Context: what did the author believe the requirements were? Was the design discussed beforehand?
- The person: how experienced are they with React, and what do they want to grow in?
The answers decide what is a merge blocker, what becomes a ticket, and what is a teaching moment.
2. Triage by impact
Take a typical submission: a profile component typed with any, a fetch inside useEffect(..., []) that reads a prop, an API key in the URL, a bio rendered as raw HTML, a clickable div for "Edit", and list items without keys.
| Tier | Finding | Why it matters |
|---|---|---|
| Blocker, security | Raw HTML rendering of user content | Stored XSS |
| Blocker, security | Secret or token in client code or query string | Leaks via bundle and logs |
| Blocker, correctness | Effect uses a prop but has empty deps | Stale data when the prop changes |
| Blocker, correctness | No res.ok check, no loading or error state | Silent failure, broken UI on 4xx/5xx |
| High, correctness | No cancellation | Out-of-order responses overwrite newer ones; state set after unmount |
| High, accessibility | Click handler on a non-interactive element | Not focusable, no role, invisible to screen readers |
| Medium, performance | Missing list keys | Reconciliation bugs, extra renders |
| Medium, maintainability | any types; derived value copied into state | No type safety; two sources of truth |
| Low, style | Short names, export style | Let the linter handle it |
Label each comment explicitly (for example issue (blocking):, suggestion:, nit:) so the author knows where to spend effort.
3. Give feedback that teaches
- Open with a summary. Acknowledge what works, name two or three themes, and say plainly which items must be fixed before merge.
- Critique the code, not the person. "This effect reads
idbut does not list it as a dependency" lands better than "you forgot the deps." - Question vs. prescribe. Use questions when reasoning is the lesson ("What happens if the user switches from profile A to B before A's response arrives?"). State the fix directly when it is non-negotiable, as with XSS or secrets, and link to authoritative docs.
- Explain why, and link one good resource instead of many small corrections.
- Do not flood the PR. Comment once on a repeated pattern ("same applies elsewhere"). Automate style with lint and format rules.
- Switch to a live conversation once there are more than about five substantive comments. A 30-minute pairing session beats a wall of red text.
4. Concrete refactors
- Separate data from presentation. Move fetching into a hook or the team's server-state library. It should include the id in the cache key, pass an abort signal, check
res.ok, and expose loading and error states.
function useUser(id: string) {
return useQuery({
queryKey: ['user', id],
queryFn: async ({ signal }) => {
const res = await fetch(`/api/users/${id}`, { signal, credentials: 'include' });
if (!res.ok) throw new Error(`Failed to load user (${res.status})`);
return (await res.json()) as User;
},
});
}
- Small, typed presentational component. Give it an explicit
Propstype, render loading, error and empty states, and compute derived values (such as a full name) during render instead of storing them. - Keep secrets on the server. Use cookie-based or proxied auth rather than shipping keys to the browser.
- Render user content safely. Render it as text, or sanitize it (for example with DOMPurify) if HTML is truly required.
- Use a real
<button>for actions, with a descriptive label. - Validate at the boundary with a schema (Zod) or generated types, so API changes fail loudly in one place.
- Naming and structure: descriptive names, stable keys (
post.id, not the index), and a route-level error boundary for unexpected render failures.
5. Tests to request
Tie each test to a real defect found in the review.
- Hook/unit: success on 200, error on 404/500, no refetch when the id is unchanged; tests for any pure formatting helpers.
- Component (React Testing Library plus a network mock such as MSW):
- Loading state, then the rendered name.
- Error message on a 500.
- Race regression: change the id while the first request is delayed; the late response must not overwrite the newer data.
- XSS: a bio containing a script tag renders inert.
- Accessibility: the edit control is found with
getByRole('button', { name: /edit/i }), is operable by keyboard, and calls the callback with the user. Add an automated axe check.
- End-to-end (sparingly): one happy-path flow, such as opening a profile and starting an edit. Keep the suite small so it stays fast and stable.
6. Follow-up
- Pair on the trickiest part (effects and async races), letting the author drive.
- Capture the lessons in a short team doc or PR checklist: security basics, data-fetching pattern, accessibility minimums.
- Automate prevention: ESLint rules for hook dependencies and
no-explicit-any, jsx-a11y, a secret scanner, type-checking in CI, and a PR template. - Check in later: review the next PR with the same themes, call out visible improvement, and agree on one growth goal.
- Share the load: if the issues came from unclear requirements or missing guidance, fix that process rather than blaming the author.
Common mistakes
- Treating every comment as equal, which hides the blockers.
- Rewriting the code for them instead of explaining.
- Approving a security issue to be kind, or blocking on pure style to be thorough.
- Asking only for tests of the happy path.
- Reviewing in isolation without asking about context and deadlines.