{"id":33,"date":"2025-11-14T03:34:45","date_gmt":"2025-11-14T03:34:45","guid":{"rendered":"https:\/\/tech.jenson.in\/?p=33"},"modified":"2025-11-14T03:34:46","modified_gmt":"2025-11-14T03:34:46","slug":"25-essential-react-interview-questions-answers","status":"publish","type":"post","link":"https:\/\/tech.jenson.in\/index.php\/2025\/11\/14\/25-essential-react-interview-questions-answers\/","title":{"rendered":"25 Essential React Interview Questions &#038; Answers"},"content":{"rendered":"<div style=\"font-family: 'Inter', sans-serif; max-width: 800px; margin: 0 auto; color: #333;\">\n<p style=\"text-align: center; font-size: 1.1rem; color: #6c757d; margin-bottom: 40px;\">\nStudy core concepts, performance, and best practices to ace your next React interview.\n<\/p>\n<p><!-- Question 1 --><\/p>\n<h2 style=\"color: #003366; font-size: 1.5rem; margin-top: 30px; border-bottom: 2px solid #e9ecef; padding-bottom: 5px;\">1. What are React Hooks and what problem do they solve?<\/h2>\n<p>React <strong>Hooks<\/strong> are functions that let you &#8216;hook into&#8217; React state and lifecycle features from function components. They were introduced in React 16.8 to allow developers to write stateful logic without using classes, solving issues like confusing <code>this<\/code> binding, difficulty reusing stateful logic between components, and the complexity of lifecycle methods in classes.<\/p>\n<p><!-- Question 2 --><\/p>\n<h2 style=\"color: #003366; font-size: 1.5rem; margin-top: 30px; border-bottom: 2px solid #e9ecef; padding-bottom: 5px;\">2. Explain the Virtual DOM (VDOM) and Reconciliation process.<\/h2>\n<p>The <strong>Virtual DOM<\/strong> is a lightweight copy of the real DOM. When state changes, React updates the VDOM first. <strong>Reconciliation<\/strong> is the process where React&#8217;s <strong>Diffing Algorithm<\/strong> compares the new VDOM with the previous one. It calculates the minimum number of changes needed and batches them to update the real DOM efficiently, which is generally much faster than direct DOM manipulation.<\/p>\n<p><!-- Question 3 --><\/p>\n<h2 style=\"color: #003366; font-size: 1.5rem; margin-top: 30px; border-bottom: 2px solid #e9ecef; padding-bottom: 5px;\">3. Differentiate between Controlled and Uncontrolled Components.<\/h2>\n<p>In <strong>Controlled Components<\/strong>, form data (e.g., input values) is handled by the React component&#8217;s state. The state is the &#8216;single source of truth&#8217; for the input value, updated via an <code>onChange<\/code> handler. In <strong>Uncontrolled Components<\/strong>, form data is handled by the DOM itself. You use a <strong><code>ref<\/code><\/strong> to access the value directly when needed (e.g., on form submission). Controlled components are preferred for immediate validation and dynamic input handling.<\/p>\n<p><!-- Question 4 --><\/p>\n<h2 style=\"color: #003366; font-size: 1.5rem; margin-top: 30px; border-bottom: 2px solid #e9ecef; padding-bottom: 5px;\">4. Describe the purpose of <code>useEffect<\/code> and its dependency array.<\/h2>\n<p>The <strong><code>useEffect<\/code><\/strong> Hook allows you to perform side effects (data fetching, subscriptions, manually changing the DOM, timers) in function components. The <strong>dependency array<\/strong> (<code>[]<\/code>) controls when the effect runs: <br \/>&#8211; <strong>Empty array (<code>[]<\/code>):<\/strong> Runs once after the initial render (like <code>componentDidMount<\/code>).<br \/>&#8211; <strong>No array:<\/strong> Runs after every render.<br \/>&#8211; <strong>Array with values (<code>[a, b]<\/code>):<\/strong> Runs on the initial render and whenever <code>a<\/code> or <code>b<\/code> change.<\/p>\n<p><!-- Question 5 --><\/p>\n<h2 style=\"color: #003366; font-size: 1.5rem; margin-top: 30px; border-bottom: 2px solid #e9ecef; padding-bottom: 5px;\">5. What is the difference between state and props?<\/h2>\n<p><strong>Props<\/strong> (properties) are used to pass data from a parent component to a child component. They are read-only and immutable. <strong>State<\/strong> is a private object that is managed within a component (using <code>useState<\/code>). It holds data that may change over time and causes the component to re-render when updated.<\/p>\n<p><!-- Question 6 --><\/p>\n<h2 style=\"color: #003366; font-size: 1.5rem; margin-top: 30px; border-bottom: 2px solid #e9ecef; padding-bottom: 5px;\">6. What is prop drilling and how can you avoid it?<\/h2>\n<p><strong>Prop drilling<\/strong> is the process of passing data (props) through several layers of intermediate components that don&#8217;t actually need the data, simply to get it to a deeply nested child component. It can be avoided using <strong>Context API<\/strong> (for application-wide state), or state management libraries like Redux\/Zustand, or by using <strong>Component Composition<\/strong> (passing a component as a prop).<\/p>\n<p><!-- Question 7 --><\/p>\n<h2 style=\"color: #003366; font-size: 1.5rem; margin-top: 30px; border-bottom: 2px solid #e9ecef; padding-bottom: 5px;\">7. Explain the concept of Higher-Order Components (HOCs).<\/h2>\n<p>A <strong>Higher-Order Component (HOC)<\/strong> is an advanced React technique for reusing component logic. It is a function that takes a component as an argument and returns a new component with enhanced functionality (e.g., data fetching, authentication checks, logging). HOCs are slowly being replaced by custom Hooks.<\/p>\n<p><!-- Question 8 --><\/p>\n<h2 style=\"color: #003366; font-size: 1.5rem; margin-top: 30px; border-bottom: 2px solid #e9ecef; padding-bottom: 5px;\">8. When should you use <code>useMemo<\/code> and <code>useCallback<\/code>?<\/h2>\n<p><strong><code>useMemo<\/code><\/strong> is used to memoize the result of an expensive calculation, preventing it from being re-calculated on every render unless its dependencies change. <strong><code>useCallback<\/code><\/strong> is used to memoize a function definition itself, which is crucial when passing callback functions to optimized child components to prevent unnecessary re-renders of the child.<\/p>\n<p><!-- Question 9 --><\/p>\n<h2 style=\"color: #003366; font-size: 1.5rem; margin-top: 30px; border-bottom: 2px solid #e9ecef; padding-bottom: 5px;\">9. What is Context API and when should you use it?<\/h2>\n<p>The <strong>Context API<\/strong> provides a way to share values (like user authentication status, themes, or locale data) between components without having to explicitly pass a prop through every level of the component tree (avoiding prop drilling). You should use it for data that is considered &#8216;global&#8217; for a tree of React components.<\/p>\n<p><!-- Question 10 --><\/p>\n<h2 style=\"color: #003366; font-size: 1.5rem; margin-top: 30px; border-bottom: 2px solid #e9ecef; padding-bottom: 5px;\">10. Explain the importance of Keys when rendering lists.<\/h2>\n<p><strong>Keys<\/strong> are necessary when rendering a list of elements. They give React a stable identity for each item in the list. This allows React to efficiently update, reorder, or delete items. Keys should be unique <em>among siblings<\/em> and should ideally be a stable identifier from the data (like a database ID), <strong>not<\/strong> the array index (which can change if the list is reordered).<\/p>\n<p><!-- Question 11 --><\/p>\n<h2 style=\"color: #003366; font-size: 1.5rem; margin-top: 30px; border-bottom: 2px solid #e9ecef; padding-bottom: 5px;\">11. What is React Router and how does it enable client-side routing?<\/h2>\n<p><strong>React Router<\/strong> is a standard library used to define navigation and routing within a single-page application (SPA). It dynamically changes the rendered components based on the URL in the browser without requiring a full page refresh, simulating multi-page navigation on the client-side using the browser&#8217;s History API.<\/p>\n<p><!-- Question 12 --><\/p>\n<h2 style=\"color: #003366; font-size: 1.5rem; margin-top: 30px; border-bottom: 2px solid #e9ecef; padding-bottom: 5px;\">12. How do you handle side effects like data fetching in function components?<\/h2>\n<p>You primarily use the <strong><code>useEffect<\/code><\/strong> Hook for side effects. For data fetching, the logic is usually placed inside <code>useEffect<\/code>, and it&#8217;s essential to include an empty dependency array (<code>[]<\/code>) if you only want to fetch the data once on mount, or include relevant dependencies if the fetching logic depends on state or props.<\/p>\n<p><!-- Question 13 --><\/p>\n<h2 style=\"color: #003366; font-size: 1.5rem; margin-top: 30px; border-bottom: 2px solid #e9ecef; padding-bottom: 5px;\">13. Describe the four phases of a component&#8217;s lifecycle (Mounting, Updating, Unmounting).<\/h2>\n<p>1. <strong>Mounting:<\/strong> Component is being created and inserted into the DOM. (Hooks: <code>useState<\/code> initialization, <code>useEffect<\/code> with <code>[]<\/code>). 2. <strong>Updating:<\/strong> Component re-renders due to changes in state or props. (Hooks: Component body runs, <code>useEffect<\/code> runs). 3. <strong>Unmounting:<\/strong> Component is removed from the DOM. (Hooks: Cleanup function returned by <code>useEffect<\/code>). 4. <strong>Error Handling:<\/strong> When an error occurs during rendering, in a lifecycle method, or in a constructor.<\/p>\n<p><!-- Question 14 --><\/p>\n<h2 style=\"color: #003366; font-size: 1.5rem; margin-top: 30px; border-bottom: 2px solid #e9ecef; padding-bottom: 5px;\">14. What is JSX and why is it used?<\/h2>\n<p><strong>JSX<\/strong> (JavaScript XML) is a syntax extension for JavaScript recommended by React. It allows you to write HTML-like structures directly within your JavaScript code. It is not mandatory, but it simplifies component structure and is compiled (via Babel) into standard JavaScript function calls (<code>React.createElement<\/code>) that create React elements.<\/p>\n<p><!-- Question 15 --><\/p>\n<h2 style=\"color: #003366; font-size: 1.5rem; margin-top: 30px; border-bottom: 2px solid #e9ecef; padding-bottom: 5px;\">15. What is Server-Side Rendering (SSR) in React and its benefits?<\/h2>\n<p><strong>Server-Side Rendering (SSR)<\/strong> is when a React application&#8217;s initial rendering is performed on the server, generating full HTML, which is then sent to the browser. Benefits include <strong>Improved SEO<\/strong> (search engines see fully rendered HTML) and <strong>Faster Time-to-Content<\/strong> (the user sees content sooner, improving perceived performance).<\/p>\n<p><!-- Question 16 --><\/p>\n<h2 style=\"color: #003366; font-size: 1.5rem; margin-top: 30px; border-bottom: 2px solid #e9ecef; padding-bottom: 5px;\">16. How do you optimize performance in a React application?<\/h2>\n<p>Performance can be optimized by: 1. Using <strong><code>React.memo<\/code><\/strong> (for functional components) or <strong><code>shouldComponentUpdate<\/code><\/strong> (for class components) to prevent unnecessary re-renders. 2. Using <strong><code>useMemo<\/code><\/strong> and <strong><code>useCallback<\/code><\/strong> to memoize expensive values and functions. 3. Lazy loading components using <strong><code>React.lazy<\/code><\/strong> and <strong><code>Suspense<\/code><\/strong>. 4. Virtualization for long lists. 5. Using Keys correctly in lists.<\/p>\n<p><!-- Question 17 --><\/p>\n<h2 style=\"color: #003366; font-size: 1.5rem; margin-top: 30px; border-bottom: 2px solid #e9ecef; padding-bottom: 5px;\">17. What are Fragments and why use them?<\/h2>\n<p><strong>Fragments<\/strong> (<code>&lt;React.Fragment&gt;<\/code> or <code>&lt;&gt;...&lt;\/&gt;<\/code>) allow you to group a list of children without adding extra nodes to the DOM. Since React components must return a single element, fragments are used when you need multiple elements without wrapping them in an unnecessary <code>&lt;div&gt;<\/code>, improving DOM structure and performance.<\/p>\n<p><!-- Question 18 --><\/p>\n<h2 style=\"color: #003366; font-size: 1.5rem; margin-top: 30px; border-bottom: 2px solid #e9ecef; padding-bottom: 5px;\">18. What is <code>React.memo<\/code>?<\/h2>\n<p><strong><code>React.memo<\/code><\/strong> is a Higher-Order Component (HOC) used to optimize functional components. If the component receives the same props as the previous render, <code>React.memo<\/code> will skip rendering the component and reuse the last rendered result, preventing unnecessary re-renders.<\/p>\n<p><!-- Question 19 --><\/p>\n<h2 style=\"color: #003366; font-size: 1.5rem; margin-top: 30px; border-bottom: 2px solid #e9ecef; padding-bottom: 5px;\">19. How does the <code>useReducer<\/code> Hook differ from <code>useState<\/code>?<\/h2>\n<p><strong><code>useReducer<\/code><\/strong> is an alternative to <code>useState<\/code> for state management, particularly useful for handling <strong>complex state logic<\/strong> that involves multiple sub-values or when the next state depends on the previous one. It takes a <strong>reducer function<\/strong> (similar to Redux) and an initial state, and returns the current state and a <code>dispatch<\/code> function.<\/p>\n<p><!-- Question 20 --><\/p>\n<h2 style=\"color: #003366; font-size: 1.5rem; margin-top: 30px; border-bottom: 2px solid #e9ecef; padding-bottom: 5px;\">20. What is a React Portal?<\/h2>\n<p>A <strong>Portal<\/strong> provides a way to render children into a DOM node that exists outside the DOM hierarchy of the parent component. This is commonly used for modal dialogs, tooltips, or popovers, allowing them to escape the parent&#8217;s overflow or styling restrictions.<\/p>\n<p><!-- Question 21 --><\/p>\n<h2 style=\"color: #003366; font-size: 1.5rem; margin-top: 30px; border-bottom: 2px solid #e9ecef; padding-bottom: 5px;\">21. Code Evaluation: What is the output of this component and why?<\/h2>\n<p>The output will be <strong>&#8216;Count: 1&#8217;<\/strong>. This is because <code>setCount(count + 1)<\/code> and <code>setCount(count + 1)<\/code> are both executed within the same render cycle (the click handler). React batches state updates, so both calls see the same initial value of <code>count<\/code> (which is 0). The result is that <code>0 + 1<\/code> is applied twice, but since it&#8217;s the same update, it only increments once.<\/p>\n<p><!-- Question 22 --><\/p>\n<h2 style=\"color: #003366; font-size: 1.5rem; margin-top: 30px; border-bottom: 2px solid #e9ecef; padding-bottom: 5px;\">22. Code Evaluation: How would you fix the previous component to increment twice?<\/h2>\n<p>You must use the <strong>functional form<\/strong> of the state update (<code>setCount(prevCount =&gt; prevCount + 1)<\/code>). This functional form ensures that the update uses the latest available state value, bypassing the batching issue and correctly incrementing the count by two.<\/p>\n<div style=\"background-color: #f1f1f1; padding: 15px; border-radius: 4px; overflow-x: auto; font-size: 0.9rem; border-left: 4px solid #003366; margin-top: 15px;\">\n<h4 style=\"color: #003366; font-size: 1rem; margin-bottom: 10px;\">Code Example:<\/h4>\n<pre style=\"white-space: pre-wrap; word-wrap: break-word;\"><code>\/\/ Before:\n\n\nconst handleClick = () => {\nsetCount(count + 1); \/\/ 0 + 1 = 1\nsetCount(count + 1); \/\/ 0 + 1 = 1\n};\n\n\/\/ After (Corrected):\nconst handleClick = () => {\nsetCount(prevCount => prevCount + 1); \/\/ 0 -> 1\nsetCount(prevCount => prevCount + 1); \/\/ 1 -> 2\n};<\/code><\/pre>\n<p style=\"background-color: #fff3cd; border-left: 3px solid #ffeeba; padding: 10px; margin-top: 15px; color: #856404; font-weight: 500;\"><strong>Evaluation:<\/strong> The functional update form (<code>prevCount => ...<\/code>) ensures sequential updates based on the correct previous state, resulting in &#8216;Count: 2&#8217;.<\/p>\n<\/div>\n<p><!-- Question 23 --><\/p>\n<h2 style=\"color: #003366; font-size: 1.5rem; margin-top: 30px; border-bottom: 2px solid #e9ecef; padding-bottom: 5px;\">23. Code Evaluation: What is wrong with this <code>useEffect<\/code> dependency array?<\/h2>\n<p>The dependency array is missing <code>myCount<\/code> and <code>myValue<\/code>. According to the <strong>Rules of Hooks<\/strong>, any variable or function defined outside of the effect (and used inside the effect) must be included in the dependency array. If <code>myCount<\/code> changes, the effect will use a stale value of <code>myCount<\/code> from the initial render, causing a bug.<\/p>\n<div style=\"background-color: #f1f1f1; padding: 15px; border-radius: 4px; overflow-x: auto; font-size: 0.9rem; border-left: 4px solid #003366; margin-top: 15px;\">\n<h4 style=\"color: #003366; font-size: 1rem; margin-bottom: 10px;\">Code Example:<\/h4>\n<pre style=\"white-space: pre-wrap; word-wrap: break-word;\"><code>function MyComponent({ myValue }) {\n\n\nconst [myCount, setCount] = useState(0);\n\nuseEffect(() => {\nconsole.log(myCount, myValue);\n\/\/ Effect uses myCount and myValue but dependency array is empty\n}, []);\n\n\/\/ ...\n}<\/code><\/pre>\n<p style=\"background-color: #fff3cd; border-left: 3px solid #ffeeba; padding: 10px; margin-top: 15px; color: #856404; font-weight: 500;\"><strong>Evaluation:<\/strong> The <code>useEffect<\/code> dependency array should be <code>[myCount, myValue]<\/code> to prevent stale closure issues and ensure the effect re-runs when those values change.<\/p>\n<\/div>\n<p><!-- Question 24 --><\/p>\n<h2 style=\"color: #003366; font-size: 1.5rem; margin-top: 30px; border-bottom: 2px solid #e9ecef; padding-bottom: 5px;\">24. What are custom Hooks and provide an example use case.<\/h2>\n<p>A <strong>custom Hook<\/strong> is a JavaScript function whose name starts with &#8216;use&#8217; (e.g., <code>useFetch<\/code>, <code>useLocalStorage<\/code>). It&#8217;s used to extract and share stateful logic between different components without relying on HOCs or render props. <strong>Example Use Case:<\/strong> A <code>useLocalStorage<\/code> Hook to abstract the logic for reading and writing state to the browser&#8217;s <code>localStorage<\/code>.<\/p>\n<p><!-- Question 25 --><\/p>\n<h2 style=\"color: #003366; font-size: 1.5rem; margin-top: 30px; border-bottom: 2px solid #e9ecef; padding-bottom: 5px;\">25. What is Strict Mode and what benefits does it provide?<\/h2>\n<p><strong>Strict Mode<\/strong> is a tool for highlighting potential problems in an application. It does <strong>not<\/strong> render any visible UI but activates extra checks and warnings for its descendants. It helps in: 1. Detecting deprecated feature usage. 2. Highlighting potentially unsafe lifecycles in class components. 3. Warning about legacy string ref API usage. 4. Double-invoking effects\/constructors to check for unintended side effects.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Study core concepts, performance, and best practices to ace your next React interview. 1. What are React Hooks and what problem do they solve? React Hooks are functions that let you &#8216;hook into&#8217; React state and lifecycle features from function components. They were introduced in React 16.8 to allow developers to write stateful logic without [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-33","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/tech.jenson.in\/index.php\/wp-json\/wp\/v2\/posts\/33","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tech.jenson.in\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech.jenson.in\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech.jenson.in\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tech.jenson.in\/index.php\/wp-json\/wp\/v2\/comments?post=33"}],"version-history":[{"count":2,"href":"https:\/\/tech.jenson.in\/index.php\/wp-json\/wp\/v2\/posts\/33\/revisions"}],"predecessor-version":[{"id":36,"href":"https:\/\/tech.jenson.in\/index.php\/wp-json\/wp\/v2\/posts\/33\/revisions\/36"}],"wp:attachment":[{"href":"https:\/\/tech.jenson.in\/index.php\/wp-json\/wp\/v2\/media?parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.jenson.in\/index.php\/wp-json\/wp\/v2\/categories?post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.jenson.in\/index.php\/wp-json\/wp\/v2\/tags?post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}