<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kumara Swamy on Medium]]></title>
        <description><![CDATA[Stories by Kumara Swamy on Medium]]></description>
        <link>https://medium.com/@kumaraswamykushi?source=rss-a0223c1ba049------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*iLSM-7dVDwNWrcAFzBFjsQ.jpeg</url>
            <title>Stories by Kumara Swamy on Medium</title>
            <link>https://medium.com/@kumaraswamykushi?source=rss-a0223c1ba049------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 10:07:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kumaraswamykushi/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[React Top Interview Questions and Solutions]]></title>
            <link>https://medium.com/@kumaraswamykushi/react-top-interview-questions-and-solutions-2d463524b229?source=rss-a0223c1ba049------2</link>
            <guid isPermaLink="false">https://medium.com/p/2d463524b229</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-hook]]></category>
            <category><![CDATA[react-js-interview]]></category>
            <category><![CDATA[reactjs]]></category>
            <dc:creator><![CDATA[Kumara Swamy]]></dc:creator>
            <pubDate>Tue, 29 Oct 2024 06:50:21 GMT</pubDate>
            <atom:updated>2024-10-29T07:27:58.460Z</atom:updated>
            <content:encoded><![CDATA[<h3>Top Interview Questions and Solutions</h3><h4>1. What is React, and why is it popular?</h4><p><strong>React</strong> is an open-source library focused on building user interfaces, especially single-page applications. Its component-based architecture enables developers to create reusable UI elements, while React’s Virtual DOM optimizes rendering, making applications fast and responsive.</p><h4>2. What is JSX in React?</h4><p><strong>JSX</strong> stands for JavaScript XML. It allows you to write HTML-like syntax in JavaScript, which React then compiles to React.createElement() calls. JSX makes code more readable and easier to write when building UI components.</p><h4>3. Explain the difference between State and Props in React.</h4><ul><li><strong>State</strong>: A local, mutable object managed within a component, representing data that can change over time.</li><li><strong>Props</strong>: Short for “properties,” props are read-only inputs passed from a parent to child components, making data flow in React unidirectional.</li></ul><h4>4. What are Hooks, and why are they significant in React?</h4><p><strong>Hooks</strong> are a feature in React introduced in version 16.8 that allow developers to use state and other React features in functional components without the need for class components.</p><h4>Key Points:</h4><ol><li><strong>State Management</strong>: The useState hook lets you add state to functional components.</li><li><strong>Side Effects</strong>: The useEffect hook manages side effects, such as data fetching, after component renders.</li><li><strong>Context API</strong>: The useContext hook simplifies the use of context, avoiding prop drilling.</li><li><strong>Custom Hooks</strong>: You can create custom hooks to encapsulate and reuse stateful logic.</li><li><strong>Cleaner Code</strong>: Hooks promote less code duplication and better organization of component logic.</li></ol><h4>Significance:</h4><ul><li><strong>Simplifies Logic</strong>: Hooks make component logic easier to manage.</li><li><strong>Supports Functional Components</strong>: They allow stateful logic in simpler functional components.</li><li><strong>Encourages Reusability</strong>: Custom hooks enable sharing of logic across components.</li><li><strong>Reduces Complexity</strong>: Eliminates the need for classes and binding this.</li></ul><p>Overall, hooks enhance the way React developers build applications, making code more concise and maintainable.</p><h4>5. What is the Virtual DOM? How does it improve React’s performance?</h4><p>The <strong>Virtual DOM</strong> is a lightweight, in-memory representation of the actual DOM, used by React to optimize UI updates. Rather than directly modifying the DOM with every state change (which can be slow), React creates a virtual copy of the DOM to manage updates more efficiently.</p><h4>How the Virtual DOM Works:</h4><ol><li><strong>Render</strong>: When a component’s state or props change, React renders a new Virtual DOM representing the updated UI.</li><li><strong>Diffing</strong>: React compares the new Virtual DOM with the previous one to detect changes (a process known as “diffing”).</li><li><strong>Reconciliation</strong>: React identifies the minimal number of real DOM updates needed based on this comparison and only applies those specific changes.</li></ol><h4>How the Virtual DOM Improves Performance:</h4><p>The Virtual DOM allows React to <strong>batch and minimize real DOM updates</strong>, which are typically slower due to reflows and repaints in the browser. By updating only the changed elements, React achieves smoother, faster user experiences, especially in applications with dynamic, complex UIs.6. Can you explain component lifecycle methods?</p><h4>6. React class components follow a lifecycle:</h4><h4>Mounting (when the component is added to the DOM):</h4><ul><li><strong>constructor()</strong>: Initializes state and binds methods.</li><li><strong>componentDidMount()</strong>: Runs after the component appears in the DOM, good for data fetching or initializing side effects.</li></ul><h4>Updating (when state or props change):</h4><ul><li><strong>shouldComponentUpdate()</strong>: Decides if the component should re-render.</li><li><strong>componentDidUpdate()</strong>: Executes after the component updates in the DOM, often used for reacting to changes.</li></ul><h4>Unmounting (when the component is removed from the DOM):</h4><ul><li><strong>componentWillUnmount()</strong>: Handles cleanup, such as canceling timers or removing event listeners.</li></ul><h4>7. What does useEffect do in React?</h4><p>In React, <strong>useEffect</strong> is a hook that allows you to perform <strong>side effects</strong> in functional components. Side effects are actions like data fetching, setting up subscriptions, or modifying the DOM that are triggered by state or prop changes.</p><h4>How useEffect Works:</h4><ul><li><strong>Syntax</strong>: useEffect(() =&gt; { /* effect code */ }, [dependencies]);</li><li><strong>Effect Code</strong>: The function inside useEffect runs after each render by default. You can use it to perform actions after the component renders or state changes.</li><li><strong>Dependencies Array</strong>: Adding dependencies in the array [ ] controls when the effect runs:</li><li><strong>Empty Array </strong><strong>[ ]</strong>: Runs the effect only once, after the initial render.</li><li><strong>With Dependencies </strong><strong>[dep1, dep2]</strong>: Runs the effect only when specified dependencies change.</li><li><strong>No Array</strong>: Runs on every render, which can lead to performance issues if not managed properly.</li></ul><p>Example Usage:</p><pre>useEffect(() =&gt; {<br>  // Fetch data or interact with the DOM<br>  return () =&gt; {<br>    // Cleanup code, like canceling subscriptions<br>  };<br>}, [dependencies]);</pre><h4>Common Uses:</h4><ul><li>Fetching data from APIs</li><li>Subscribing to or cleaning up events</li><li>Updating the DOM in response to changes</li></ul><p>The <strong>cleanup function</strong> inside useEffect (returned by the effect) helps prevent memory leaks by handling any necessary cleanup when the component unmounts or the dependencies change.</p><h4>8. What is Redux, and how does it integrate with React?</h4><p><strong>Redux</strong> is a state management library for JavaScript applications, widely used with React to manage app-wide state in a predictable way. Redux enables consistent state across components, making debugging and testing easier, especially in complex applications.</p><h4>9. Describe the Context API and its benefits.</h4><p>The <strong>Context API</strong> allows data to be shared across the component tree without needing to pass props through each level. It’s beneficial for global data, such as user settings or themes, which multiple components may need access to.</p><p>Simple Example:</p><pre>import { createContext, useState } from &quot;react&quot;;<br><br>export const ThemeContext = createContext();<br><br>export function ThemeProvider({ children }) {<br>  const [theme, setTheme] = useState(&#39;light&#39;);<br><br>  const toggleTheme = () =&gt; {<br>    setTheme((currentTheme) =&gt; (currentTheme === &#39;light&#39; ? &#39;dark&#39; : &#39;light&#39;));<br>  };<br><br>  return (<br>    &lt;ThemeContext.Provider value={{ toggleTheme, theme }}&gt;<br>      {children}<br>    &lt;/ThemeContext.Provider&gt;<br>  );<br>}</pre><h4>10. What is a Higher-Order Component (HOC) in React?</h4><p>An <strong>HOC</strong> is a function that takes a component and returns a new component with additional props or functionality. It’s useful for reusing component logic, such as authentication, logging, or data fetching, across various parts of an application.</p><h4>11. Why are keys necessary when rendering lists in React?</h4><p><strong>Keys</strong> help React identify and track which items have changed, been added, or removed. Assigning a unique key to each list item ensures the DOM is updated efficiently, improving app performance.</p><h4>12. What is useMemo, and when would you use it?</h4><p><strong>useMemo</strong> is a hook that memoizes the output of a function to avoid unnecessary recalculations. It’s useful for performance optimization in cases where an expensive computation depends on specific dependencies.</p><h4>13. Explain lazy loading in React.</h4><p><strong>Lazy loading</strong> is a technique where components or resources are only loaded when they’re needed, enhancing performance. React’s React.lazy function enables code-splitting by loading components on-demand, making applications faster.</p><h4>14. What is PropTypes, and how is it used in React?</h4><p><strong>PropTypes</strong> is a library that helps with type-checking props in components, ensuring components receive data in the correct format. This practice reduces errors and improves debugging.</p><h4>15. Comparison of React’s class component lifecycle methods and their equivalents in functional components using hooks:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H4Mrs7-5C-8_XH36RHSCJQ.png" /></figure><h4>16. React Hooks and release versions</h4><p>React Hooks were released in <strong>React 16.8</strong> (February 2019) and have evolved across subsequent versions. Here’s an overview of the main hooks introduced in <strong>React 16.8</strong> and additional ones in later versions:</p><h4>React 16.8 (Initial Release of Hooks)</h4><p><strong>Core Hooks</strong> introduced:</p><blockquote><strong>useState</strong>: Manages state in functional components.</blockquote><blockquote><strong>useEffect</strong>: Performs side effects like data fetching, subscriptions, etc.</blockquote><blockquote><strong>useContext</strong>: Consumes context values in functional components.</blockquote><p><strong>Additional Utility Hooks</strong>:</p><blockquote><strong>useReducer</strong>: Alternative to useState for managing more complex state logic.</blockquote><blockquote><strong>useCallback</strong>: Memoizes a function to prevent re-creation on re-renders.</blockquote><blockquote><strong>useMemo</strong>: Memoizes a computed value, recalculating only when dependencies change.</blockquote><blockquote><strong>useRef</strong>: Accesses or stores a mutable reference across renders.</blockquote><blockquote><strong>useLayoutEffect</strong>: Similar to useEffect but fires synchronously after all DOM mutations.</blockquote><blockquote><strong>useImperativeHandle</strong>: Customizes instance values exposed to parent components via ref.</blockquote><p><strong>Custom Hooks</strong>: Developers could now create their own custom hooks to encapsulate and reuse logic.</p><h4>React 16.9</h4><p><strong>useDebugValue</strong>: Introduced for custom hooks to display debugging information in React DevTools.</p><h4>React 17</h4><ul><li>No new hooks were introduced in React 17. Instead, this release focused on <strong>backward compatibility</strong>, <strong>event delegation improvements</strong>, and preparing for the major changes in React 18.</li></ul><h4>React 18 (Concurrent Rendering and New Hooks)</h4><p>React 18 introduced <strong>concurrent rendering</strong> and added new hooks to support it.</p><p><strong>New Hooks</strong>:</p><blockquote><strong>useTransition</strong>: Defers state updates to keep the UI responsive by marking them as non-urgent.</blockquote><blockquote><strong>useDeferredValue</strong>: Defers a value until the UI can render it without blocking more urgent updates.</blockquote><blockquote><strong>useId</strong>: Generates unique IDs for accessibility attributes or keys, preventing ID clashes in concurrent rendering.</blockquote><blockquote><strong>useSyncExternalStore</strong>: Provides a way to safely read and subscribe to external stores in concurrent rendering.</blockquote><blockquote><strong>useInsertionEffect</strong>: A new hook for injecting styles before browser layout and paint, useful for CSS-in-JS libraries.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d463524b229" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>