8 questions
React interview questions and answers
Junior questions
1. How do props differ from state in React?
Answer
A component receives props from its parent, and for that component they are read only. State is the component's own data, which it manages itself with
useStateoruseReducer. A change of state or new props causes a re-render. If data can be computed from props, it does not need separate state.Follow-up question: Why can you not mutate state directly, for example
state.items.push(x)?2. Why do you need a
keywhen rendering lists?Answer
A
keyhelps React match list items between renders and understand what was added, removed or reordered. The key must be stable and unique among siblings, ideally an id from the data. Using the array index as a key breaks item state and animations when the list is sorted, filtered or items are inserted at the start. Without a key React shows a warning in the console.Follow-up question: How can you deliberately reset a component's state with
key?3. How does
useEffectwork, and what does the dependency array mean?Answer
useEffectruns a side effect after render: a request, a subscription, a timer. With no dependency array the effect runs after every render, with an empty array only after mount, and with values whenever one of them changes. The function returned by the effect is the cleanup: React calls it before the next run of the effect and on unmount. Cleanup is where you unsubscribe, clear timers and cancel requests.Follow-up question: Why does an effect run twice in development with StrictMode?
4. What are controlled and uncontrolled form inputs?
Answer
In a controlled input the value lives in state and is passed through
value, while changes come throughonChange. React is then the single source of truth, which makes validation or formatting while typing easy. In an uncontrolled input the value lives in the DOM, you read it through arefor on submit, and set the initial value withdefaultValue. Uncontrolled inputs are simpler and re-render less, but suit complex logic less well.Follow-up question: What happens if you pass
valuewithoutonChange?5. How do you share data between two sibling components?
Answer
Shared state is lifted to the closest common parent and passed down through props together with functions that change it. If the data is needed deep in the tree and passing props becomes awkward, you use Context. Context fits data that changes rarely, like the theme or the current user. For complex global state you pick a dedicated library such as Zustand or Redux.
Follow-up question: Why can a change of a Context value cause unnecessary re-renders?
Middle questions
1. What makes a component re-render?
Answer
A component renders again when its state changes, when its parent renders, or when a Context it reads changes. By default a parent render re-renders all its children, even if their props did not change.
React.memoskips the render if the props are shallowly equal. A render does not mean a DOM change: React updates the DOM only where the output differs.Follow-up question: Why does
React.memonot help if you pass a new object in props on every render?2. What are
useMemoanduseCallbackfor? When should you not use them?Answer
useMemoremembers the result of a computation, anduseCallbackremembers the function itself, until the dependencies change. They help with expensive computations and with stable references passed toReact.memocomponents or touseEffectdependencies. Memoization has its own cost: memory and comparing dependencies. Wrapping everything makes no sense, first find a real problem in the profiler.Follow-up question: How does the React Compiler change the need for manual memoization?
3. What is reconciliation, and how does React decide what to update in the DOM?
Answer
Reconciliation is the process of comparing the new element tree with the previous one. React compares the trees heuristically: if the element type at the same position changed, the subtree is destroyed and recreated together with its state. If the type is the same, React updates only the changed attributes and recurses into the children. For lists it matches items by
key, and then applies the minimal set of changes to the DOM.Follow-up question: Why does declaring a component inside another component reset its state on every render?
Train these questions
Study the cards, answer by text or voice and get an AI grade. Needs a signed in account.
Answer out loud and get graded
Here you get the questions with model answers. In the ShortLesson app you answer yourself, by text or voice, and AI grades the answer against the rubric and asks a follow-up, like a real interviewer.
The app is coming to the stores soon.