Interviews / Frontend Engineering / This data fetching component has a race condition. How do you fix it?
A critical component is crashing in production. How would you debug it?
This statically generated page is showing stale data. How do you fetch and display live data on the client?
This component is failing its test. Fix the component to make the test pass.
This form doesn't reset correctly. Fix the bug and refactor to a more robust solution.
An expensive component is re-rendering unnecessarily. How would you fix this state management issue?
This data fetching component has a race condition. How do you fix it?
This component causes a hydration mismatch error. Why and how to fix it?
This component has hardcoded text. Refactor it to support multiple languages.
This component is not reusable. Refactor it using the composition pattern.
This legacy class component mixes logic and UI. Refactor it to a modern functional component using Hooks.
Why does this component log the old state value after calling a useState setter?
A child component is failing to update the parent's state. Why is this happening and how do you fix it?
Clarify the core React concepts: Element, Component, and Node.
Your app's state management is a mess. How would Flux architecture help?
Your UI freezes during large updates. How does React Fiber help?
This list re-renders inefficiently. How does React's reconciliation algorithm explain why?
This component makes duplicate API calls in development. How does Strict Mode help identify the bug?
Your team is evaluating React. How would you explain its core benefits?
This component has accessibility ID issues. Fix it with `useId`.
This modal is clipped by its parent. Fix it using a React Portal.
This component re-renders unnecessarily due to context object reference changes. How do you fix it?
This button should increment twice, but only increments once. How do you fix it?
An expensive component is re-rendering unnecessarily. How would you fix this state management issue?
This component flickers when resizing. Fix it using the correct effect hook.
This data fetching component is causing an infinite loop. How do you fix it?
This component is crashing with a JSX syntax error. How do you fix it?
This component is breaking layout. Fix it using a React Fragment.
This component is trying to modify a prop directly. How do you fix it?
This application suffers from 'prop drilling.' How do you fix it?
This component is crashing due to a 'Rules of Hooks' violation. How do you fix it?
This custom component is not forwarding its ref. How do you fix it?
This `useEffect` is logging a stale value. How do you fix its dependency array?
This component's UI is not updating. Why is direct state mutation problematic?
This component has issues managing a timer ID. Fix it with `useRef`.
This optimized component re-renders unnecessarily. How do you fix it with `useCallback`?
This large component is slowing down initial load. Implement code splitting.
This component is crashing the entire app. Implement an Error Boundary to gracefully handle the error.
This component performs expensive calculations unnecessarily. Optimize it with `useMemo`.
This component re-renders unnecessarily. How do you prevent it?
This component's state logic is complex. Refactor it with `useReducer`.
These components duplicate logic. Refactor them using a Higher-Order Component.
These components duplicate logic. Refactor them using the Render Props pattern.
These components duplicate stateful logic. Refactor them using a custom Hook.
This component manages loading state imperatively. Refactor it to use React Suspense.
This form uses an uncontrolled input. Refactor it to be a controlled component.
Questions
Q
C
Q
Q
C
Q
C
Q
C
Q
C
Q
C
Q
C
Q
C
Q
C
Q
C
Q
C
Q
C
Q
C
Q
C
Q
C
Q
C
Q
C
Q
C
Q
C
Q
C
Q
C
Q
C
Q
C
Q
C
Q
C
Q
C
This data fetching component has a race condition. How do you fix it?
Wrong Approach
I would debounce the input to prevent rapid fetching.
Right Approach
Debouncing helps reduce requests, but doesn't fix the core issue. The real problem is that useEffect doesn't cancel previous in-flight requests. We need to use a cleanup function with AbortController.
The Race Condition Sequence:
userIdbecomes “2” → fetch starts (2000ms delay)userIdbecomes “3” → new fetch starts (500ms delay)- Fetch “3” resolves first → UI shows “Charlie” ✓
- Fetch “2” resolves late → UI overwrites to “Bob” ✗
The Fix: Return a cleanup function from useEffect that aborts the previous request when dependencies change.
UserDetails Component - Fix the Race Condition
Your Code
import React, { useState, useEffect } from 'react';
// Mock API with variable delay
const fetchUser = (id) => {
const users = { '1': { name: 'Alice' }, '2': { name: 'Bob' }, '3': { name: 'Charlie' } };
const delay = id === '2' ? 2000 : 500;
return new Promise(resolve => {
setTimeout(() => resolve(users[id]), delay);
});
};
export default function UserDetails() {
const [userId, setUserId] = useState('1');
const [user, setUser] = useState(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
setLoading(true);
// THE BUG: No cleanup - previous fetch will overwrite state
fetchUser(userId).then(fetchedUser => {
setUser(fetchedUser);
setLoading(false);
});
}, [userId]);
return (
<div>
<input type="text" value={userId} onChange={e => setUserId(e.target.value)} />
<hr />
{loading ? <p>Loading...</p> : user && <h2>{user.name}</h2>}
</div>
);
} Click "Run Code" to see output...
Click "Run Code" to see test results...
The Solution
useEffect(() => {
const controller = new AbortController();
setLoading(true);
fetchUser(userId, controller.signal)
.then(fetchedUser => {
setUser(fetchedUser);
setLoading(false);
})
.catch(error => {
if (error.name === 'AbortError') return; // Expected
setLoading(false);
});
// Cleanup: abort when userId changes or unmounts
return () => controller.abort();
}, [userId]);
Practice Question
In a useEffect hook, what is the primary purpose of the returned cleanup function?