Interviews / Frontend Engineering / This component is crashing due to a 'Rules of Hooks' violation. 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 component is crashing due to a 'Rules of Hooks' violation. How do you fix it?
Wrong Approach
Maybe wrap useState in a try-catch to handle the error?
Right Approach
The useState is inside an if statement, which violates the Rules of Hooks. React relies on the call order of hooks being the same on every render. Conditional hooks break this:
The Rule: Don’t call Hooks inside loops, conditions, or nested functions.
Why It Matters: React tracks hooks by their call order. If a hook is called conditionally, the order changes between renders, corrupting React’s internal state tracking.
The Fix: Always call hooks at the top level. Use conditional logic after the hook call.
ConditionalCounter - Fix the Rules of Hooks Violation
Your Code
import React, { useState } from 'react';
export default function ConditionalCounter({ showCounter }) {
// THE BUG: useState inside a condition!
if (showCounter) {
const [count, setCount] = useState(0); // This crashes!
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
return <p>Counter is hidden.</p>;
}
function App() {
const [show, setShow] = useState(true);
return (
<div>
<button onClick={() => setShow(!show)}>Toggle Counter</button>
<ConditionalCounter showCounter={show} />
</div>
);
} Click "Run Code" to see output...
Click "Run Code" to see test results...
The Solution
Option 1: Always call the hook, conditionally render the output
export default function ConditionalCounter({ showCounter }) {
// Always call hooks at the top level
const [count, setCount] = useState(0);
if (!showCounter) {
return <p>Counter is hidden.</p>;
}
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
Option 2: Conditionally render the entire component
function Counter() {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(c => c + 1)}>Count: {count}</button>;
}
function App() {
const [show, setShow] = useState(true);
return show ? <Counter /> : <p>Hidden</p>;
}
Practice Question
Which of the following is a valid rule for using React Hooks?