<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mahdar</title>
    <description>The latest articles on DEV Community by Mahdar (@mahdardavari).</description>
    <link>https://dev.to/mahdardavari</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1160507%2F501ed95b-293b-43b7-ad4c-7b61c5804c43.jpeg</url>
      <title>DEV Community: Mahdar</title>
      <link>https://dev.to/mahdardavari</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mahdardavari"/>
    <language>en</language>
    <item>
      <title>Why useRef Is Better Than querySelector in React</title>
      <dc:creator>Mahdar</dc:creator>
      <pubDate>Mon, 22 Dec 2025 13:55:33 +0000</pubDate>
      <link>https://dev.to/mahdardavari/why-useref-is-better-than-queryselector-in-react-5145</link>
      <guid>https://dev.to/mahdardavari/why-useref-is-better-than-queryselector-in-react-5145</guid>
      <description>&lt;p&gt;If you come from a vanilla JavaScript or jQuery background, &lt;code&gt;document.querySelector&lt;/code&gt; probably feels natural. You want a DOM node you grab it. Simple.&lt;/p&gt;

&lt;p&gt;But React is not &lt;em&gt;DOM-first.&lt;/em&gt;&lt;br&gt;
It’s &lt;em&gt;state-first&lt;/em&gt; and &lt;em&gt;declarative&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;querySelector&lt;/code&gt; inside a React app is often a code smell. It might work today, but it silently breaks React’s mental model and makes your code fragile in ways that only show up later.&lt;/p&gt;

&lt;p&gt;Let’s talk about why &lt;code&gt;useRef&lt;/code&gt; exists, what problem it actually solves, and why it’s the correct tool in React.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;React Does Not Own the DOM the Way You Think&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In React, &lt;strong&gt;you do not control the DOM directly&lt;/strong&gt;. React does.&lt;/p&gt;

&lt;p&gt;React maintains its own abstraction (the Virtual DOM) and decides:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;when elements are created&lt;/li&gt;
&lt;li&gt;when they are destroyed&lt;/li&gt;
&lt;li&gt;when they are replaced&lt;/li&gt;
&lt;li&gt;when they are re-used&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you use querySelector, you are bypassing React and reaching into the DOM manually. That creates a hidden dependency between your code and something React considers implementation detail.&lt;/p&gt;

&lt;p&gt;This leads to subtle bugs.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;useRef Is a Contract With React&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you use &lt;code&gt;useRef&lt;/code&gt;, you are not “querying” the DOM.&lt;/p&gt;

&lt;p&gt;You are saying to React:&lt;br&gt;
“When you create this element, please give me a stable reference to it.”&lt;br&gt;
That’s a contract, not a hack.&lt;br&gt;
React guarantees that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the ref will point to the correct DOM node&lt;/li&gt;
&lt;li&gt;the reference will stay stable across re-renders&lt;/li&gt;
&lt;li&gt;the ref lifecycle is aligned with the component lifecycle&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;This is something querySelector can never guarantee.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;useRef&lt;/code&gt;, React assigns the reference only after the element is committed to the DOM.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Concurrent Rendering Changes Everything&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;React’s concurrent features (like &lt;code&gt;Suspense&lt;/code&gt;, transitions, streaming SSR) make DOM querying even more dangerous.&lt;/p&gt;

&lt;p&gt;In concurrent rendering:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;React can render multiple versions of the UI&lt;/li&gt;
&lt;li&gt;Only one version is committed&lt;/li&gt;
&lt;li&gt;Others are thrown away&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you use &lt;code&gt;querySelector&lt;/code&gt;, you might accidentally interact with a DOM node from a render that never gets committed.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useRef&lt;/code&gt; always points to the &lt;strong&gt;committed tree&lt;/strong&gt; the one the user actually sees.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Performance Is Not the Main Point (But It Helps)&lt;/strong&gt;&lt;br&gt;
This isn’t really about performance but it’s worth mentioning.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;querySelector&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;walks the DOM&lt;/li&gt;
&lt;li&gt;depends on selector complexity&lt;/li&gt;
&lt;li&gt;can become expensive if abused&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;useRef&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;is a direct pointer&lt;/li&gt;
&lt;li&gt;no lookup&lt;/li&gt;
&lt;li&gt;constant-time access&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;More importantly, refs avoid unnecessary reflows and re-queries caused by repeated DOM searches.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Final Thoughts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Using querySelector in React is like bypassing the type system in TypeScript it works, until it doesn’t.&lt;/p&gt;

&lt;p&gt;useRef:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;respects React’s lifecycle&lt;/li&gt;
&lt;li&gt;works with concurrent rendering&lt;/li&gt;
&lt;li&gt;keeps components isolated&lt;/li&gt;
&lt;li&gt;makes intent explicit&lt;/li&gt;
&lt;li&gt;scales with your codebase&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As your app grows, these things stop being “best practices” and start being requirements.&lt;/p&gt;

&lt;p&gt;Write React the way React expects to be written.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>Top 30 React Interview Questions and Answers in 2025</title>
      <dc:creator>Mahdar</dc:creator>
      <pubDate>Sun, 30 Nov 2025 13:31:10 +0000</pubDate>
      <link>https://dev.to/mahdardavari/top-30-react-interview-questions-and-answers-in-2025-29hc</link>
      <guid>https://dev.to/mahdardavari/top-30-react-interview-questions-and-answers-in-2025-29hc</guid>
      <description>&lt;p&gt;This guide breaks down the core concepts that define the modern React ecosystem, focusing on the game-changing features in &lt;strong&gt;React 19&lt;/strong&gt; and the &lt;strong&gt;Server Component&lt;/strong&gt; Architecture.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;1. What are React Server Components (RSCs) and how do they work?&lt;/strong&gt;&lt;br&gt;
React Server Components (RSCs) represent a &lt;strong&gt;fundamental paradigm shift&lt;/strong&gt; in how we build React applications. They allow components to be rendered &lt;strong&gt;exclusively on the server&lt;/strong&gt;, shifting work out of the client-side bundle and execution environment. They are a complement to, not a replacement for, traditional Client Components.&lt;/p&gt;

&lt;p&gt;How they work:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Zero-Bundle-Size:&lt;/strong&gt; RSC code, including large dependencies like date libraries or Markdown parsers, is &lt;strong&gt;never shipped to the client&lt;/strong&gt;. This drastically reduces the JavaScript bundle size.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Server-Only Execution:&lt;/strong&gt; They run entirely on the server. They cannot use state (&lt;code&gt;useState&lt;/code&gt;), effects (&lt;code&gt;useEffect&lt;/code&gt;), or browser-only APIs (like &lt;code&gt;onClick&lt;/code&gt;). They are primarily for &lt;strong&gt;data fetching&lt;/strong&gt; and &lt;strong&gt;static UI composition&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Direct Backend Access:&lt;/strong&gt; They can securely and directly access backend resources (databases, file systems, internal services) without the performance overhead or complexity of a separate REST/GraphQL layer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://vercel.com/blog/understanding-react-server-components" rel="noopener noreferrer"&gt;Understanding React Server Components&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Integrated Rendering:&lt;/strong&gt; The React framework (Next.js App Router, Remix, etc.) orchestrates the rendering. It sends the RSC output as a lightweight "UI description" to the client, while reserving standard client-side JavaScript for interactive elements.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Composition:&lt;/strong&gt; RSCs can &lt;strong&gt;import and render Client Components&lt;/strong&gt; seamlessly, but the reverse is not true (Client Components cannot import Server Components).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; A &lt;code&gt;ProductPage&lt;/code&gt; Server Component handles the secure data fetch and renders the static layout, which then imports an interactive &lt;code&gt;AddToCartButton&lt;/code&gt; Client Component.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;2. What is the React Compiler in React 19?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The React Compiler (formerly "React Forget") is a new, automatic compiler designed to fundamentally solve the problem of unnecessary re-renders in React. Its primary goal is to &lt;strong&gt;automatically manage memoization&lt;/strong&gt;, making manual calls to &lt;code&gt;useMemo&lt;/code&gt;, &lt;code&gt;useCallback&lt;/code&gt;, and &lt;code&gt;React.memo&lt;/code&gt; obsolete.&lt;/p&gt;

&lt;p&gt;How it works:&lt;br&gt;
The compiler deeply analyzes your component code and applies memoization only where it's truly beneficial. It ensures that components only re-render when the UI's semantic meaning has actually changed&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Before (Manual):&lt;/strong&gt; Developers had to correctly identify and wrap logic/values to prevent unnecessary re-creation on every render.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Manual memoization and dependency array management&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;expensiveValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;calculate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;After (Automatic):&lt;/strong&gt; The compiler ensures that the variable &lt;code&gt;expensiveValue&lt;/code&gt; retains its identity and value across renders if its dependencies (&lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;) haven't changed—without any manual wrapping.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// The compiler does the memoization automatically&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;expensiveValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;calculate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This change is critical: it leads to less boilerplate, eliminates a major source of performance bugs, and allows developers to write cleaner, more standard JavaScript. &lt;a href="https://react.dev/learn/react-compiler/introduction" rel="noopener noreferrer"&gt;What does React Compiler do? &lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;3. What are Actions in React 19?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Actions are a first-class feature for managing data mutations and submissions, typically triggered by a form. They are designed to integrate natively with Server Components, providing a robust, built-in solution for form handling, pending states, and optimistic updates.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://vercel.com/blog/whats-new-in-react-19" rel="noopener noreferrer"&gt;Whats-new-in-react-19?&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How they work:&lt;br&gt;
An async function—the "Action"—is passed directly to a form element's action prop. This function can execute on the server (Server Action) or the client. React manages the entire submission lifecycle: preventing default behavior, handling loading states, and managing errors.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// A Server Component&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;AddToCart&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;productId&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// The 'use server' directive marks this function for server-side execution.&lt;/span&gt;
  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;addToCartAction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;formData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use server&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;itemId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;formData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;itemId&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;addToCart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;itemId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Securely mutate data on the server&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;form&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;addToCartAction&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Pass the async function as the action */&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hidden&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;itemId&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;submit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Add&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;Cart&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/form&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;4. What is the &lt;code&gt;useActionState&lt;/code&gt; Hook?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;useActionState&lt;/code&gt; hook (previously &lt;code&gt;useFormState&lt;/code&gt;) is the client-side counterpart to Actions. It allows a component to handle the state and response that results from an Action submission.&lt;br&gt;
&lt;a href="https://vercel.com/blog/whats-new-in-react-19#useactionstate" rel="noopener noreferrer"&gt;useActionState&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;use client&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useActionState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createUser&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./actions&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;initialState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Signup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;formAction&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pending&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useActionState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;initialState&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;form&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;formAction&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt; &lt;span class="nx"&gt;htmlFor&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;email&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/label&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;email&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;email&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;required&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* ... */&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="nx"&gt;aria&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;live&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;polite&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;}
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;aria&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;disabled&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;pending&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;submit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;pending&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Submitting...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sign up&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/form&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;5. What is useFormStatus Hook?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;useFormStatus&lt;/code&gt; hook is a simple way to access the submission status of the nearest parent &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt; that is executing an Action.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://vercel.com/blog/whats-new-in-react-19#useformstatus" rel="noopener noreferrer"&gt;useformstatus&lt;/a&gt; &lt;br&gt;
How it works:&lt;/p&gt;

&lt;p&gt;This hook provides metadata about the form submission, primarily the &lt;code&gt;pending&lt;/code&gt; boolean, which is ideal for disabling the submit button or showing a loading spinner. Crucially, it must be used inside a &lt;strong&gt;Client Component&lt;/strong&gt; that is a descendant of the &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt; being processed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useFormStatus&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-dom&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;SubmitButton&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;pending&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useFormStatus&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;submit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;disabled&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;pending&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;pending&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Submitting...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Save Changes&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;6. What is useOptimistic Hook?&lt;/strong&gt;&lt;br&gt;
The &lt;code&gt;useOptimistic&lt;/code&gt; hook is dedicated to implementing &lt;strong&gt;optimistic UI updates&lt;/strong&gt;. It allows the application to show the user the &lt;strong&gt;expected result&lt;/strong&gt; of a mutation instantly, even before the network request has completed, significantly improving &lt;a href="https://react.dev/reference/react/useOptimistic" rel="noopener noreferrer"&gt;perceived performance.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How it works:&lt;/p&gt;

&lt;p&gt;It maintains two versions of state: the &lt;strong&gt;real state&lt;/strong&gt; and the &lt;strong&gt;optimistic state&lt;/strong&gt;. When a mutation starts, you update the optimistic state immediately. When the server response is received (success or error), the optimistic state automatically syncs back to the real state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useOptimistic&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MessageList&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;messages&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;optimisticMessages&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;addOptimisticMessage&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useOptimistic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// The real state (from server)&lt;/span&gt;
    &lt;span class="c1"&gt;// The function to compute the optimistic state&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentMessages&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;newMessageText&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;currentMessages&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;newMessageText&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;sending&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}]&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;formAction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;formData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newMessageText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;formData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;message&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;addOptimisticMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newMessageText&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Optimistically update the UI&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sendMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newMessageText&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// The real server request (takes time)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;optimisticMessages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sending&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;small&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Sending&lt;/span&gt;&lt;span class="p"&gt;...)&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/small&amp;gt;}&amp;lt;/&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;))}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;form&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;formAction&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/form&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;7. What is Concurrent Rendering in React?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Concurrent Rendering is a foundational feature introduced in React 18 that makes the rendering process &lt;strong&gt;interruptible&lt;/strong&gt;. It allows React to work on multiple states or "versions" of the UI simultaneously, pausing, resuming, or abandoning a render based on priority. &lt;a href="https://www.angularminds.com/blog/optimizing-performance-with-react-suspense-and-concurrent-mode" rel="noopener noreferrer"&gt;more...&lt;/a&gt;&lt;br&gt;
Key Benefit:&lt;/p&gt;

&lt;p&gt;It prevents the main thread from being blocked by non-urgent updates, ensuring the UI remains highly responsive (e.g., typing in an input field is always smooth) even when a heavy, lower-priority update is processing in the background.&lt;/p&gt;

&lt;p&gt;Mechanisms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Slicing:&lt;/strong&gt; Breaking rendering work into small chunks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Suspense:&lt;/strong&gt; Allowing components to declare they are "waiting" for data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;useTransition:&lt;/strong&gt; A hook to explicitly mark a state update as non-urgent, allowing high-priority updates (like input events) to interrupt it.&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;&lt;strong&gt;8. What are the 'use client' and 'use server' directives?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These are mandatory directives that explicitly define the execution environment within the React Server Component architecture.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;'use client' Environment -&amp;gt; Client (Browser)&lt;/code&gt;: To create an interactive component.Can use state, effects, and all browser APIs (e.g., onClick, localStorage).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;'use server' Environment -&amp;gt; Server (Node.js)&lt;/code&gt;: To create a secure, mutable API endpoint (Action).Can access backend resources (database, file system) but cannot use browser APIs.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Placement:
&lt;code&gt;use client&lt;/code&gt; is placed at the very top of a file.&lt;code&gt;use server&lt;/code&gt; is typically placed at the top of an &lt;strong&gt;async function&lt;/strong&gt; within an RSC, turning that function into a Server Action callable from the client.&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;&lt;strong&gt;9. How does the new use API work?&lt;/strong&gt;&lt;br&gt;
The &lt;code&gt;use&lt;/code&gt; API is a special, &lt;strong&gt;non-conditional&lt;/strong&gt; hook (though it looks like one, it can be called inside conditionals and loops, unlike standard hooks) that reads the value of a resource like a &lt;strong&gt;Promise&lt;/strong&gt; or &lt;strong&gt;Context&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Key Difference and Value:&lt;/p&gt;

&lt;p&gt;It simplifies Suspense integration. When &lt;code&gt;use&lt;/code&gt; is called with a Promise, it forces the nearest Suspense boundary to show its fallback until the Promise resolves.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Reading a Promise (simplifies Suspense integration)&lt;/span&gt;
&lt;span class="c1"&gt;// The component will suspend until the notePromise resolves.&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Note&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;notePromise&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;note&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;notePromise&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;note&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Reading Context&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Sidebar&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Unlike traditional hooks, `use` can be called conditionally&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;shouldShowSidebar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;theme&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ThemeContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;10. What is Document Metadata in React 19?&lt;/strong&gt;&lt;br&gt;
React 19 now offers native support for document metadata, allowing developers to render elements like &lt;code&gt;&amp;lt;title&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;meta&amp;gt;&lt;/code&gt;, and &lt;code&gt;&amp;lt;link&amp;gt;&lt;/code&gt; anywhere in the component tree.&lt;/p&gt;

&lt;p&gt;How it works:&lt;/p&gt;

&lt;p&gt;React's renderer automatically detects and &lt;strong&gt;hoists&lt;/strong&gt; these tags, ensuring they are correctly placed into the document's &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; section during both initial SSR and client-side rendering. This drastically simplifies managing SEO-critical metadata, especially in complex, nested routing/layout structures.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;BlogPost&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;post&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;article&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Rendering metadata anywhere in the component tree */&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;My&lt;/span&gt; &lt;span class="nx"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/title&amp;gt;&lt;/span&gt;&lt;span class="err"&gt; 
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;meta&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;author&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;author&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* ... rest of the content */&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/article&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;11. What improvements were made to refs in React 19?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The primary improvement is a significant simplification of ref handling: you can now pass &lt;code&gt;ref&lt;/code&gt; as a regular prop to function components without needing &lt;code&gt;forwardRef&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;12. What is the Activity component in React 19?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;&amp;lt;Activity&amp;gt;&lt;/code&gt; component in React 19 solves a critical problem: how to hide parts of your UI without losing state. It's like putting components to sleep while keeping their memory intact. &lt;a href="https://medium.com/@ignatovich.dm/react-19-2-activity-component-300025b76883" rel="noopener noreferrer"&gt;more...&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;13. What is useEffectEvent Hook?&lt;/strong&gt;&lt;br&gt;
At its core, the &lt;code&gt;useEffectEvent&lt;/code&gt; hook allows you to create stable event handlers within effects. These handlers always have access to the latest state and props, even without being included in the effect’s dependency array. However, on a broader scope, the Hook provides a solution to a subtle but common challenge that affects how effects in React handle the stale closure problem.&lt;a href="https://blog.logrocket.com/react-useeffectevent/" rel="noopener noreferrer"&gt;more...&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;14. How does Automatic Batching work in React 19?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Automatic Batching&lt;/strong&gt; is a performance optimization where React groups multiple state updates (&lt;code&gt;setX&lt;/code&gt;, &lt;code&gt;setY&lt;/code&gt;) that happen within a single execution cycle into a &lt;strong&gt;single&lt;/strong&gt;, &lt;strong&gt;unified&lt;/strong&gt; &lt;strong&gt;re-render&lt;/strong&gt;.&lt;br&gt;
Automatic Batching is applied to all state updates, regardless of their origin (event handlers, promises, timeouts, native code, etc.). This ensures that any sequence of updates always results in only &lt;strong&gt;one&lt;/strong&gt; re-render, by default.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// In React 18 and 19, this sequence triggers only ONE re-render.&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;New Name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// Even if setName was in a setTimeout, it would be batched.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;15. What is Partial Pre-rendering in React?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Partial Pre-rendering is a key performance strategy enabled by RSCs and Suspense. &lt;u&gt;It allows the server to initially send a page as &lt;strong&gt;static HTML&lt;/strong&gt; while leaving "holes" for highly dynamic or personalized content.&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;How it works:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Static Shell:&lt;/strong&gt; The page's static structure and layout are served instantly (like a pre-rendered static site, excellent for Core Web Vitals).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Suspense "Holes":&lt;/strong&gt; Dynamic sections are wrapped in &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt;boundaries. The server sends a lightweight placeholder for these.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Streaming:&lt;/strong&gt; As the server fetches the data for the dynamic parts, it streams the completed HTML for those sections separately, replacing the placeholders without blocking the rest of the page.&lt;br&gt;
This delivers the &lt;strong&gt;SEO&lt;/strong&gt; and &lt;strong&gt;performance benefits&lt;/strong&gt; of Static Site Generation (SSG) combined with the freshness and dynamism of Server-Side Rendering (SSR).&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;16. How has SSR improved in React 19?&lt;/strong&gt;&lt;br&gt;
The Server Component architecture fundamentally redefined and improved Server-Side Rendering (SSR) by moving data fetching to the server side.&lt;br&gt;
Key Improvements:&lt;br&gt;
&lt;strong&gt;Elimination of Client-Side Data Waterfall:&lt;/strong&gt; Components fetch data directly on the server, eliminating the need to wait for the client to download JS, hydrate, and then start fetching data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Intelligent Streaming &amp;amp; Hydration:&lt;/strong&gt; React can stream the UI in smaller, prioritized chunks. The client starts hydrating and becoming interactive on the critical, ready-to-go parts while the slower parts are still streaming in the background.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unified Architecture:&lt;/strong&gt; The line between SSG and SSR is blurred. The framework (like Next.js) can cache RSC outputs intelligently, only re-rendering and re-streaming the parts that need to be fresh.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;17. What are Performance Tracks in React?&lt;/strong&gt;&lt;br&gt;
"Performance Tracks" is a general, non-official term that refers to the practice of monitoring and analyzing the various stages of an application's lifecycle, especially focusing on &lt;strong&gt;rendering performance and bundle size.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Key Tools for Tracking Performance:&lt;br&gt;
&lt;strong&gt;React DevTools Profiler:&lt;/strong&gt; The primary tool for tracing component renders, measuring render time, and identifying unnecessary re-renders (which the React Compiler aims to eliminate).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Browser DevTools (Performance Tab):&lt;/strong&gt; Essential for analyzing CPU usage, repaint/layout times, and identifying long tasks that block the main thread.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Splitting/Bundle Analysis:&lt;/strong&gt; Using tools like Webpack Bundle Analyzer to break down and optimize the size of the JavaScript sent to the client.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;18. How do keys work in React lists and why use unique IDs?&lt;/strong&gt;&lt;br&gt;
The &lt;code&gt;key&lt;/code&gt; prop is a special string attribute required when rendering a list of elements. It helps React identify which items in a list have changed, been added, or been removed, which is crucial for efficient DOM manipulation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Unique and Stable IDs are Non-Negotiable:&lt;/strong&gt;&lt;br&gt;
React uses the &lt;code&gt;key&lt;/code&gt; to create a stable identity for each component in the list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using Index as Key (Anti-Pattern):&lt;/strong&gt; If a list's order changes (e.g., an item is prepended or sorted), every item's index/key changes. React sees this as &lt;strong&gt;every component being deleted and re-created,&lt;/strong&gt; leading to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Significant &lt;strong&gt;performance degradation&lt;/strong&gt; due to unnecessary DOM manipulation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;State bugs&lt;/strong&gt; where component state (like the value of an input field) is incorrectly retained or mixed up across the wrong list items.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Using Unique ID as Key (Best Practice):&lt;/strong&gt; By using a stable, unique identifier from your data (e.g., &lt;code&gt;item.id&lt;/code&gt;), the identity of each item is preserved regardless of where it moves in the list, ensuring correct, efficient updates.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;19. What's the difference between controlled and uncontrolled components?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This distinction relates to how form input data is managed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Controlled Component:&lt;/strong&gt; The form data is entirely handled by React's component state (&lt;code&gt;useState&lt;/code&gt;).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Mechanism:&lt;/strong&gt; The input's value is set by the &lt;code&gt;value&lt;/code&gt; prop, and changes are handled by an &lt;code&gt;onChange&lt;/code&gt; handler that updates the state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt; Single source of truth (the React state), easy to perform immediate validation and conditional formatting, and clear data flow.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;- Example:&lt;/strong&gt;&lt;br&gt;
 &lt;code&gt;&amp;lt;input value={state} onChange={handleChange} /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Uncontrolled Component:&lt;/strong&gt; The form data is managed by the DOM itself (&lt;em&gt;standard HTML behavior&lt;/em&gt;).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Mechanism:&lt;/strong&gt; You use a &lt;code&gt;ref&lt;/code&gt; to access the value imperatively when you need it (e.g., in a submit handler). You typically use the &lt;code&gt;defaultValue&lt;/code&gt; prop for initial state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pros:&lt;/strong&gt; Requires less boilerplate code, simpler for very basic forms or integrating non-React libraries.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;- Example:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;&amp;lt;input ref={inputRef} defaultValue="Initial Value" /&amp;gt;&lt;/code&gt;&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;20. What are best practices for state management?&lt;/strong&gt;&lt;br&gt;
The state management landscape is heavily influenced by the Server Component architecture, shifting the responsibility for data fetching and caching away from client-side global stores.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Prioritize Server Components for Data:&lt;/strong&gt; For any server-derived data (products, users, posts), the best practice is to fetch it directly in an &lt;strong&gt;RSC&lt;/strong&gt;. This eliminates the need for complex global client-side caching solutions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Colocate Local UI State:&lt;/strong&gt; Use &lt;code&gt;useState&lt;/code&gt; and &lt;code&gt;useReducer&lt;/code&gt; for all local, ephemeral state (e.g., modal open/close, dropdown status). Keep state as close to its usage as possible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Choose Lightweight for Global Client State:&lt;/strong&gt; For complex, cross-cutting UI state (e.g., dark mode toggle, multi-step form state), favor minimal, low-boilerplate libraries like &lt;strong&gt;Zustand&lt;/strong&gt; or &lt;strong&gt;Jotai&lt;/strong&gt; over heavy solutions like Redux.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Use Specialized Libraries for Advanced Server Caching:&lt;/strong&gt; If you have highly dynamic, frequently updated data or need sophisticated features like offline support and focus-refetching, &lt;strong&gt;TanStack Query (React Query)&lt;/strong&gt; remains the gold standard for managing server cache on the client.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Leverage the URL for Global State:&lt;/strong&gt; Use the URL and &lt;code&gt;search parameters&lt;/code&gt; (via hooks like useSearchParams in Next.js/Remix) to manage state that should be persisted, shareable, and refresh-safe (e.g., filters, sorting, active tab).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Summary:&lt;/strong&gt; The modern stack is about decentralization. Server Components handle data, and React's built-in hooks handle local UI logic. Global stores are reserved only for truly complex, cross-application client-side state.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;21. What is hydration in React?&lt;/strong&gt;&lt;br&gt;
Hydration is the process by which React attaches event listeners and updates the DOM to match the virtual DOM on the client side. Hydration is necessary for server-side rendered React applications to ensure that the client-side rendered content is interactive and matches the server-rendered content. During hydration, React reconciles the server-rendered HTML with the client-side virtual DOM and updates the DOM to match the virtual DOM structure.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.greatfrontend.com/questions/quiz/explain-what-react-hydration-is" rel="noopener noreferrer"&gt;Read more about it here&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;22. Explain higher-order components (HOCs)?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Higher-order components (HOCs) are functions that take a component as an argument and return a new component with enhanced functionality. HOCs are used to share code between components, add additional props or behavior to components, and abstract common logic into reusable functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;withLogger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;WrappedComponent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Component rendered:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;WrappedComponent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;WrappedComponent&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;EnhancedComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;withLogger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;23. What is the purpose of the useImperativeHandle hook in React?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;useImperativeHandle&lt;/code&gt; hook in React is used to customize the instance value that is exposed to parent components when using &lt;code&gt;React.forwardRef&lt;/code&gt;. It allows you to define which properties or methods of a child component's instance should be accessible to parent components when using a ref.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ChildComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forwardRef&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;inputRef&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;useImperativeHandle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;focus&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;inputRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;focus&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;}));&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;inputRef&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ParentComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;childRef&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;childRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;focus&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ChildComponent&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;childRef&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Focus&lt;/span&gt; &lt;span class="nx"&gt;Input&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;useImperativeHandle&lt;/code&gt; hook is used to expose the &lt;code&gt;focus&lt;/code&gt; method of the &lt;code&gt;inputRef&lt;/code&gt; to the parent component when using a ref.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;24. What are the benefits and limitations of using React's Context API for state management?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Simplifies State Sharing:&lt;/strong&gt; Allows easy sharing of state across components without prop drilling.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Eliminates Prop Drilling:&lt;/strong&gt; Avoids passing props through multiple layers, making the code more maintainable.&lt;br&gt;
Limitations:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Inefficient for Frequent Updates:&lt;/strong&gt; Re-renders all components that consume the context on state changes, which can be inefficient for frequent updates.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Not Suitable for Large-Scale State Management:&lt;/strong&gt; For complex or large applications with deep state needs, consider using state management libraries like Redux or Zustand for better performance and scalability.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;25. What are React portals, and in what scenarios would you use them?&lt;/strong&gt;&lt;br&gt;
React portals provide a way to render children outside the parent component's DOM hierarchy. They are useful for modals, tooltips, or any UI element that needs to break out of the regular DOM flow but remain part of the React tree.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createPortal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Modal&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;modal-root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;26. How does React handle concurrent rendering with multiple updates and prioritize them?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;React uses the priority system in Concurrent Mode to schedule updates. It can break up large updates into smaller chunks and give priority to user interactions (like clicks or input) to ensure the app remains responsive.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/mahdardavari/mastering-async-concurrency-the-react-18-nextjs-paradigm-shift-30j9"&gt;more ...&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;27. You are rendering a huge Data Grid with 10,000 rows and 50 columns. Initial render is acceptable, but any scrolling or filtering results in severe Jank and frame rate drops. How do you solve this performance problem without reducing the amount of data the user sees?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The primary solution is List &lt;code&gt;Virtualization&lt;/code&gt; or &lt;code&gt;Windowing&lt;/code&gt;.&lt;br&gt;
Virtualization shrinks the DOM to a constant, manageable size and recycles DOM nodes instead of recreating the entire list.&lt;br&gt;
In a real-world scenario, I would use established libraries like &lt;code&gt;react-window&lt;/code&gt; or &lt;code&gt;react-virtualized&lt;/code&gt; to handle the complex mathematical calculations and DOM recycling efficiently.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;28. Scenario: Managing Nested Side Effects (Infinite Loops)&lt;br&gt;
Question: You have a complex component where its useEffect calls an internal function. This internal function, in turn, updates a piece of state in the parent component. What potential problem might occur here, and how do you resolve it with minimal hassle?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This scenario is a classic pattern for creating &lt;strong&gt;Infinite Re-render Loops&lt;/strong&gt; or unpredictable side effects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Problem:&lt;/strong&gt; If the inner function (called inside &lt;code&gt;useEffect&lt;/code&gt;) is defined without &lt;code&gt;useCallback&lt;/code&gt;, it gets a new Identity on every parent render. If this function is listed in the &lt;code&gt;useEffect&lt;/code&gt; dependency array, it forces the &lt;code&gt;useEffect&lt;/code&gt; to re-run on every render, which updates the parent's state, causing a new render, and the cycle continues.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Use &lt;code&gt;useCallback&lt;/code&gt;: The inner function should be memoized using &lt;code&gt;useCallback&lt;/code&gt;. This ensures the function's identity only changes when its own dependencies change, stabilizing the &lt;code&gt;useEffect&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Updater Function in useState: If the inner function only updates the parent state based on its previous value (e.g., &lt;code&gt;setCount(c =&amp;gt; c + 1&lt;/code&gt;)), you don't need to list the &lt;code&gt;setCount&lt;/code&gt; function in the dependency array, as its identity is guaranteed by React.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Best Practice (React 19+): Use &lt;code&gt;useEffectEvent&lt;/code&gt; (as previously discussed) to completely decouple the function from the &lt;code&gt;useEffect&lt;/code&gt; dependencies while still safely accessing the latest props and state.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;29. What is BroadcastChannel and when do you use it?&lt;/strong&gt;&lt;br&gt;
BroadcastChannel is a modern Web API that allows real-time communication between multiple browser tabs, windows, or iframes of the same origin. It’s extremely useful when you need cross-tab state synchronization without relying on heavy solutions like WebSockets or server-side events. I typically use BroadcastChannel to synchronize authentication state (e.g., instant logout across all tabs), theme changes, language changes, or any global UI settings. It provides a simple publish/subscribe model, low latency messaging, and avoids the side effects of the localStorage ‘storage’ event. Overall, it’s the cleanest and most reliable way to keep multiple browser contexts in sync.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;30. How to Search for an ID or Attribute Inside a Large String ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Depending on the data format, I use different strategies.&lt;br&gt;
For simple substrings, I prefer &lt;code&gt;includes()&lt;/code&gt; or &lt;code&gt;indexOf()&lt;/code&gt; because they are extremely fast.&lt;br&gt;
For patterns, I use regex.&lt;br&gt;
If the data is HTML, I parse it with DOMParser and query it.&lt;br&gt;
If it’s JSON, I convert it to an object and search through it.&lt;br&gt;
For huge datasets, I use streaming or build an index for O(1) lookup.&lt;br&gt;
And in performance-critical cases, I still use classic &lt;code&gt;for&lt;/code&gt; loops.&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>interview</category>
      <category>react</category>
    </item>
    <item>
      <title>Mastering Async &amp; Concurrency: The React 18+ &amp; Next.js Paradigm Shift</title>
      <dc:creator>Mahdar</dc:creator>
      <pubDate>Thu, 13 Nov 2025 10:10:41 +0000</pubDate>
      <link>https://dev.to/mahdardavari/mastering-async-concurrency-the-react-18-nextjs-paradigm-shift-30j9</link>
      <guid>https://dev.to/mahdardavari/mastering-async-concurrency-the-react-18-nextjs-paradigm-shift-30j9</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Concurrency is about dealing with a lot of things at once.&lt;br&gt;
 Asynchrony is about dealing with one thing at a time, but not blocking on it. They are related but distinct concepts, both essential for responsive systems.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;1.Concurrency vs Asynchronous — Core Concepts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Asynchronous&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt; The ability to initiate an operation and continue executing other code without waiting for that operation to complete, returning to it later. It's about "doing one thing while waiting for another to finish" without halting the main flow.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Goal:&lt;/strong&gt; Primarily, to prevent blocking the main thread, ensuring the application remains responsive, especially during I/O operations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mechanism:&lt;/strong&gt; Achieved through constructs like Promises, &lt;code&gt;async/await&lt;/code&gt; syntax, callbacks, and the browser's Web APIs or Node.js's native APIs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/api/user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// non-blocking network request&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I run while fetch is pending&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript is fundamentally single-threaded, meaning it never executes two lines of user-defined code at exactly the same time. Instead, it intelligently schedules tasks via the event loop, giving the &lt;em&gt;illusion&lt;/em&gt; of parallel work by quickly switching between tasks.&lt;/p&gt;

&lt;p&gt;In short:&lt;br&gt;
Asynchronous = "Don’t block the main thread; I’ll continue later when you’re done."&lt;/p&gt;
&lt;h2&gt;
  
  
  Concurrency
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt; The compositional aspect of being able to deal with multiple things at once. It's about managing multiple asynchronous tasks that are in progress simultaneously or interleaved, even when executed on a single processing unit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Goal:&lt;/strong&gt; To maximize the utilization of available resources (e.g., I/O bandwidth) and improve overall throughput and responsiveness by efficiently handling these multiple in-flight operations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In JS:&lt;/strong&gt; The event loop is the orchestrator. It allows multiple asynchronous operations to be "in flight" concurrently (e.g., several fetch requests waiting for network responses), even though the CPU executes only one JavaScript task at a time. The CPU switches between these tasks when one needs to wait.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/api/user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/api/posts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both network requests (&lt;code&gt;/api/user and /api/posts&lt;/code&gt;) are initiated almost concurrently. While JavaScript itself runs on one thread, the underlying network operations are handled by the browser/OS, and their results are managed together by &lt;code&gt;Promise.all&lt;/code&gt; as they resolve.&lt;/p&gt;

&lt;p&gt;In short:&lt;br&gt;
Concurrency = "Handle multiple asynchronous operations efficiently and manage their progress together."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. How This Applies in React&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;React 18+ introduced Concurrent Rendering It's crucial to understand that this is not the same as JavaScript's concurrency model (i.e., making your JavaScript code run in parallel threads). React's Concurrent Rendering is a specific, internal scheduling model that makes the process of &lt;em&gt;rendering UI&lt;/em&gt; updates interruptible and prioritizable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example: Prioritized UI Updates&lt;/strong&gt; Consider a scenario where React needs to render a large, complex component tree.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In old React (synchronous rendering):&lt;/strong&gt;  Once a render cycle started, it was monolithic and couldn't be interrupted. If a user clicked a button or typed into an input during this heavy render, the UI would freeze until the entire render completed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In Concurrent React:&lt;/strong&gt; React can now intelligently pause, abort, or reprioritize rendering work. So, if a new high-priority update (like a keyboard input event or a button click) occurs during a computationally intensive render, React can:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Pause the current lower-priority render in progress.&lt;/li&gt;
&lt;li&gt;Handle the urgent, high-priority work immediately (e.g., updating the input value). &lt;/li&gt;
&lt;li&gt;Resume or even completely restart the previous render later, without the user perceiving a freeze.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Key concurrent features that leverage this scheduling:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useTransition()&lt;/code&gt; → Mark state updates as low priority, allowing urgent updates to interrupt them.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useDeferredValue()&lt;/code&gt; → Defer re-renders for less critical parts of the UI, ensuring the primary content remains responsive.&lt;/p&gt;

&lt;p&gt;Streaming Server Components (in Next.js) → Stream HTML chunks progressively, improving perceived loading times.&lt;/p&gt;

&lt;p&gt;In short:&lt;/p&gt;

&lt;p&gt;React’s concurrency = "Smart, interruptible scheduling of UI renders for a smoother, more responsive user experience, not parallel CPU execution of your JS code."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. In Next.js (React 18+ / 19)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Next.js deeply integrates and leverages React's concurrent features to enhance both server and client-side rendering.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Server Components (RSC):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These execute asynchronously on the server, allowing you to &lt;code&gt;await&lt;/code&gt; data fetching directly within a component (&lt;code&gt;await fetch()&lt;/code&gt; in an RSC).&lt;/p&gt;

&lt;p&gt;Their rendering is concurrent, enabling React to stream parts of the UI to the client as data for each section becomes ready, rather than waiting for the entire page.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Suspense Boundaries:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Used extensively for asynchronous data fetching and code splitting. React effectively "waits" for promises to resolve before continuing rendering a specific subtree.&lt;/p&gt;

&lt;p&gt;Crucially, while waiting, it can show a predefined fallback UI, keeping the rest of the application interactive.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Streaming:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead of the traditional approach of waiting for the entire page's HTML to be generated before sending anything, React/Next.js streams chunks of HTML to the browser as they become ready.&lt;/p&gt;

&lt;p&gt;This is a direct application of concurrent rendering, allowing users to see and even interact with parts of the page much sooner.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Error Handling and Race Safety:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Concurrent React can gracefully cancel an in-progress async render if new data or navigation demands a different UI state, preventing stale UI or race conditions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5eq53ca6j6l3jvxzwbfa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5eq53ca6j6l3jvxzwbfa.png" alt="JavaScript asynchrony keeps the single main thread free by offloading work; Concurrency in JavaScript deals with efficiently managing multiple such asynchronous tasks together" width="800" height="435"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;JavaScript asynchrony keeps the single main thread free by offloading work; Concurrency in JavaScript deals with efficiently managing multiple such asynchronous tasks together; React concurrent rendering takes that same principle into the UI layer — scheduling and rendering multiple UI “tasks” concurrently and interruptibly so users always get a smooth, non-blocking experience.&lt;/p&gt;

</description>
      <category>react</category>
      <category>nextjs</category>
      <category>javascript</category>
    </item>
    <item>
      <title>React Hooks: A Comprehensive Cheatsheet &amp; Guide</title>
      <dc:creator>Mahdar</dc:creator>
      <pubDate>Mon, 03 Nov 2025 14:56:00 +0000</pubDate>
      <link>https://dev.to/mahdardavari/react-hooks-a-comprehensive-cheatsheet-guide-35f</link>
      <guid>https://dev.to/mahdardavari/react-hooks-a-comprehensive-cheatsheet-guide-35f</guid>
      <description>&lt;p&gt;React Hooks revolutionized how we write components, moving logic from complex class lifecycles to clean, reusable functions. But with so many hooks (especially with React 19!), it's easy to get lost.&lt;/p&gt;

&lt;p&gt;This post is your comprehensive cheatsheet, breaking down every hook by its purpose. &lt;strong&gt;Bookmark this page&lt;/strong&gt;—you'll come back to it!&lt;/p&gt;




&lt;h2&gt;
  
  
  The Rules of Hooks
&lt;/h2&gt;

&lt;p&gt;Before you start, remember the two golden rules (with one new exception):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Only Call Hooks at Top Level (not in loops/conditionals/nested functions)&lt;/li&gt;
&lt;li&gt;Only Call from React Functions (components or custom hooks)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;use&lt;/code&gt; hook is the exception can be called conditionally!
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ✅ Correct&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// top-level&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// ❌ Wrong&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// don't do this&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  STATE: Managing Component State
&lt;/h2&gt;

&lt;p&gt;Hooks for storing and updating values within your component.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useState&lt;/code&gt; → The basic state hook. Use it for simple, local component state (strings, numbers, booleans, arrays).&lt;br&gt;
&lt;code&gt;useReducer&lt;/code&gt; → Manages complex state logic, especially when the next state depends on the previous one. A great alternative to &lt;code&gt;useState&lt;/code&gt; for complex objects or when state transitions are well-defined (think: Redux-lite for a single component).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;increment&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useReducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ACTION &amp;amp; FORM: Hooks for Forms (React 19+)
&lt;/h2&gt;

&lt;p&gt;A new suite of hooks designed to make form handling and server actions seamless.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useActionState&lt;/code&gt; → Manages the state of a form action. It gives you the &lt;code&gt;pending&lt;/code&gt; state, the &lt;code&gt;error&lt;/code&gt; message, and the returned &lt;code&gt;data&lt;/code&gt; from the action, all in one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;formAction&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useActionState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;formData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;submitForm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;formData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;useFormStatus&lt;/code&gt; → Used inside a &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt;, this hook gives you the &lt;code&gt;pending&lt;/code&gt; status of the parent form. Perfect for disabling a submit button or showing a spinner.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useOptimistic&lt;/code&gt; → Lets you "optimistically" update the UI before an async action completes. For example, show a new message in a chat list &lt;em&gt;immediately&lt;/em&gt; while the server request is still pending. React will automatically revert it if the action fails.&lt;/p&gt;

&lt;p&gt;Optimistically update UI before async actions complete.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setMessages&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useOptimistic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;initialMessages&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleSend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newMessage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setMessages&lt;/span&gt;&lt;span class="p"&gt;([...&lt;/span&gt;&lt;span class="nx"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;newMessage&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// show instantly&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sendToServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newMessage&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// sync with backend&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  EFFECT: Side Effects &amp;amp; Lifecycle
&lt;/h2&gt;

&lt;p&gt;Hooks for interacting with the "outside world" (APIs, subscriptions, the DOM).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useEffect&lt;/code&gt; → Run side effects &lt;strong&gt;after render&lt;/strong&gt;. This is your go-to for data fetching, setting up subscriptions, or manually manipulating the DOM. Don't forget the &lt;strong&gt;cleanup function&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useEffectEvent&lt;/code&gt; → Creates a stable function to read the latest state/props inside an effect.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When to use:&lt;/strong&gt; When you need to call a function inside useEffect that uses props/state, but you don't want the effect to re-run when that prop/state changes. (Solves the "dependency array" problem).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useLayoutEffect&lt;/code&gt; → Runs &lt;strong&gt;synchronously&lt;/strong&gt; after all DOM mutations but before the browser paints. Use this only when you need to read layout from the DOM (like getting an element's size) and synchronously re-render.** Use sparingly as it blocks painting.**&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useInsertionEffect&lt;/code&gt; → Runs before &lt;code&gt;useLayoutEffect&lt;/code&gt; and before any DOM mutations. This is almost exclusively for CSS-in-JS libraries to inject &lt;code&gt;&amp;lt;style&amp;gt;&lt;/code&gt; tags. You will probably never use this directly.&lt;/p&gt;




&lt;h2&gt;
  
  
  ASYNC: Resource Loading (React 19+)
&lt;/h2&gt;

&lt;p&gt;A new paradigm for handling async data and resources.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;use&lt;/code&gt; → The revolutionary hook. It "unwraps" the value of a Promise (for data) or reads a Context. Unlike other hooks, it &lt;strong&gt;can be used in loops and conditionals&lt;/strong&gt;. It's the key to client-side data fetching with Suspense.&lt;/p&gt;




&lt;h2&gt;
  
  
  PERF: Performance &amp;amp; Memoization
&lt;/h2&gt;

&lt;p&gt;Hooks to skip unnecessary work and keep your app fast.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useMemo&lt;/code&gt; → Caches (memoizes) the result of an &lt;strong&gt;expensive calculation&lt;/strong&gt;. It re-runs the calculation only if one of its dependencies changes.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useCallback&lt;/code&gt; → Caches a function definition. This is vital when passing callbacks to optimized child components (like &lt;code&gt;React.memo&lt;/code&gt;) to prevent them from re-rendering unnecessarily.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useTransition&lt;/code&gt; → Marks state updates as "non-urgent." This keeps your UI responsive (like keeping a text input fast) while a "heavy" update (like re-rendering a large list) happens in the background.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useDeferredValue&lt;/code&gt; → Defers updating a non-critical part of the UI. Similar to &lt;code&gt;useTransition&lt;/code&gt; but used when you don't control the state update itself (e.g., the value is a prop) debouncing alternative.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// useTransition&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;isPending&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;startTransition&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useTransition&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nf"&gt;startTransition&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setFilteredItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;expensiveFilter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// useDeferredValue&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;deferredSearch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useDeferredValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;searchTerm&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Refs &amp;amp; Imperative Access
&lt;/h2&gt;

&lt;p&gt;Hooks for accessing DOM nodes or storing values that don't cause re-renders.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useRef&lt;/code&gt; → Returns a mutable ref object. Its &lt;code&gt;.current&lt;/code&gt; property can hold a value that persists across renders without triggering a re-render. Perfect for accessing DOM elements (e.g., &lt;code&gt;myInput.current.focus()&lt;/code&gt;) or storing timer IDs.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useImperativeHandle&lt;/code&gt; → Customizes the instance value that is exposed to parent components when using &lt;code&gt;ref&lt;/code&gt;. Used with &lt;code&gt;forwardRef&lt;/code&gt; to create a custom, limited API for your component.&lt;/p&gt;




&lt;h2&gt;
  
  
  OTHER: Context, Identity &amp;amp; External Stores
&lt;/h2&gt;

&lt;p&gt;Utility hooks for solving specific problems.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useContext&lt;/code&gt; → Reads and subscribes to a React Context. The simplest way to consume shared data (like a theme or user auth) and avoid "prop drilling."&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useId&lt;/code&gt; → Generates a unique, stable ID. Essential for accessibility attributes (like connecting &lt;code&gt;&amp;lt;label&amp;gt;&lt;/code&gt;s to &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt;s) and preventing ID mismatches during Server-Side Rendering (SSR).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useSyncExternalStore&lt;/code&gt; → Subscribes to an external store (like Zustand, Redux, or even window.matchMedia) in a way that is compatible with concurrent rendering.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;online&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useSyncExternalStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;online&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onLine&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;That's the list! Don't try to memorize them all. Use this guide as a reference. The best way to learn is to build.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What's your most-used&lt;/strong&gt; hook? Any tricky ones I missed? Let me know in the comments!&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
