React Interview Questions
10 questions with detailed answers
Q1. What is Virtual DOM?
Answer: A lightweight copy of the real DOM. React diffs virtual DOM changes and updates only necessary parts of the real DOM.
Q2. What are React Hooks?
Answer: Functions like useState, useEffect that let functional components use state and lifecycle features.
Q3. Difference between state and props?
Answer: Props are read-only data passed from parent; state is managed within a component and can change.
Q4. What is useEffect used for?
Answer: Side effects: data fetching, subscriptions, DOM manipulation. Runs after render.
Q5. What are keys in React lists?
Answer: Unique identifiers helping React identify which items changed, were added, or removed.
Q6. What is a controlled component?
Answer: A controlled form element receives its value from React state and reports changes through an event handler. React remains the single source of truth for the input.
Q7. When should you use useMemo and useCallback?
Answer: Use them when referential stability or avoiding an expensive recalculation has a measurable benefit. useMemo caches a computed value; useCallback caches a function reference.
Q8. What is React Context best used for?
Answer: Context distributes values such as theme, locale, or authenticated user through a component tree without repetitive prop passing. Frequently changing state may still benefit from a dedicated store.
Q9. What is hydration?
Answer: Hydration attaches React behavior and event handlers to HTML rendered on the server. The client's first render must match the server output to avoid hydration warnings.
Q10. What is an error boundary?
Answer: An error boundary catches rendering and lifecycle errors in its descendant tree and displays fallback UI. It does not catch event-handler errors, server errors, or errors inside the boundary itself.