8 questions

React interview questions and answers

Junior questions

  1. 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 useState or useReducer. 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)?

    Learn the topic in the course: React: getting started

  2. 2. Why do you need a key when rendering lists?

    Answer

    A key helps 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?

    Learn the topic in the course: React: getting started

  3. 3. How does useEffect work, and what does the dependency array mean?

    Answer

    useEffect runs 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?

    Learn the topic in the course: React: getting started

  4. 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 through onChange. 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 a ref or on submit, and set the initial value with defaultValue. Uncontrolled inputs are simpler and re-render less, but suit complex logic less well.

    Follow-up question: What happens if you pass value without onChange?

    Learn the topic in the course: React: getting started

  5. 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?

    Learn the topic in the course: React: getting started

Middle questions

  1. 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.memo skips 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.memo not help if you pass a new object in props on every render?

    Learn the topic in the course: React: getting started

  2. 2. What are useMemo and useCallback for? When should you not use them?

    Answer

    useMemo remembers the result of a computation, and useCallback remembers the function itself, until the dependencies change. They help with expensive computations and with stable references passed to React.memo components or to useEffect dependencies. 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?

    Learn the topic in the course: React: getting started

  3. 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?

    Learn the topic in the course: React: getting started

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.

Other topics