# react package

# import {useEffect} from "react";

<div class="WaaZC" id="bkmrk-the-statement-import"><div class="RJPOee EIJn2" style="animation: none !important;"><div class="rPeykc" data-hveid="CAwQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIDBAB"><span data-huuid="7831568508111237803">The statement `import {useEffect} from "react";` </span><span data-huuid="7831568508111238862">is a JavaScript import declaration used in React applications. </span></div></div></div><div class="rPeykc" data-hveid="CAwQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIDBAB" id="bkmrk-">  
</div><div class="rPeykc" data-hveid="CAwQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIDBAB" id="bkmrk--1"></div><div class="WaaZC" id="bkmrk-purpose%3A"><div class="RJPOee EIJn2" style="animation: none !important;"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CAoQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIChAB" role="heading">**<span data-huuid="7831568508111240980">Purpose: </span>**</div></div></div><div class="WaaZC" id="bkmrk-this-line-imports-th"><div class="RJPOee EIJn2" style="animation: none !important;"><div class="rPeykc" data-hveid="CA4QAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIDhAB"><span data-huuid="7831568508111239002">This line imports the `useEffect` Hook from the React library. </span><span data-huuid="7831568508111240061">The `useEffect` Hook is a fundamental part of functional components in React, enabling developers to perform "side effects." </span></div></div></div><div class="rPeykc" data-hveid="CA4QAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIDhAB" id="bkmrk--2">  
</div><div class="rPeykc" data-hveid="CA4QAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIDhAB" id="bkmrk--3"></div><div class="WaaZC" id="bkmrk-side-effects-in-reac"><div class="RJPOee EIJn2" style="animation: none !important;"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CAgQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQICBAB" role="heading">**<span data-huuid="7831568508111238083">Side Effects in React: </span>**</div></div></div><div class="WaaZC" id="bkmrk-side-effects-are-ope"><div class="RJPOee EIJn2" style="animation: none !important;"><div class="rPeykc uP58nb MNX06c" data-hveid="CAQQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIBBAB"><span data-huuid="7831568508111240201">Side effects are operations that interact with the outside world or have an impact beyond the component's render cycle. </span><span data-huuid="7831568508111237164">Common examples of side effects include: </span></div></div></div><div class="WaaZC" id="bkmrk-data-fetching%3A-makin"><div class="RJPOee EIJn2" style="animation: none !important;">- <span data-huuid="7831568508111240341">**Data fetching:** </span><span data-huuid="7831568508111237304">Making API calls to retrieve data from a server. </span>
- <span data-huuid="7831568508111239422">**DOM manipulation:** </span><span data-huuid="7831568508111240481">Directly interacting with the browser's Document Object Model (e.g., setting the document title). </span>
- <span data-huuid="7831568508111238503">**Subscriptions:** </span><span data-huuid="7831568508111239562">Setting up event listeners or connecting to external services like WebSockets. </span>
- <span data-huuid="7831568508111237584">**Timers:** </span><span data-huuid="7831568508111238643">Using `setTimeout` or `setInterval`. </span>

</div></div><div class="WaaZC" id="bkmrk-how-useeffect-works%3A"><div class="RJPOee EIJn2" style="animation: none !important;"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CCQQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIJBAB" role="heading"></div><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CCQQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIJBAB" role="heading"></div><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CCQQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIJBAB" role="heading"></div><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CCQQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIJBAB" role="heading">**<span data-huuid="11612000788613407035">How `useEffect` Works: </span>**</div></div></div><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CCQQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIJBAB" id="bkmrk--4" role="heading">  
</div><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CCQQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIJBAB" id="bkmrk--5" role="heading"></div><div class="WaaZC" id="bkmrk-useeffect-takes-two-"><div class="RJPOee EIJn2" style="animation: none !important;"><div class="rPeykc uP58nb" data-hveid="CCYQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIJhAB">**<span data-huuid="11612000788613410213"><span aria-level="2" role="heading">`useEffect` takes two arguments:</span> </span>**</div></div></div><div class="WaaZC" id="bkmrk-a-setup-function%3A-th"><div class="RJPOee EIJn2" style="animation: none !important;">- <div class="zMgcWd dSKvsb" data-il=""><div data-crb-p=""><div class="xFTqob"><div class="Gur8Ad" style="display: inline;"><span data-huuid="11612000788613408836">**A setup function:** </span></div><div class="vM0jzc" style="display: inline;"><span data-huuid="11612000788613408377">This function contains the code for your side effect. </span><span data-huuid="11612000788613407918">It runs after every render of the component (including the initial render) unless its dependencies prevent it from doing so. </span></div></div></div></div>
- <div class="zMgcWd dSKvsb" data-il=""><div data-crb-p=""><div class="xFTqob"><div class="Gur8Ad" style="display: inline;"><span data-huuid="11612000788613407000">**A dependency array (optional):** </span></div><div class="vM0jzc" style="display: inline;"><span data-huuid="11612000788613410637">This array specifies the values that the effect depends on. </span><span data-huuid="11612000788613410178">If any of these values change between renders, the effect will re-run. </span><span data-huuid="11612000788613409719">An empty dependency array `[]` ensures the effect runs only once after the initial render (similar to `componentDidMount` in class components). </span></div></div></div></div>

</div></div><div class="WaaZC" id="bkmrk-example-usage%3A"><div class="RJPOee EIJn2" style="animation: none !important;"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CDYQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQINhAB" role="heading"><span data-huuid="15663895085194306514">Example Usage: </span></div></div></div><div class="WaaZC" id="bkmrk--6"><div class="RJPOee EIJn2" style="animation: none !important;"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc"><div class="zYSUYd"><div class="FS7GEb"><div class="dDrxod"><div aria-live="polite" class="WDoJJe">  
</div></div></div></div></div></div></div></div>```
import { useEffect, useState } from "react";

function MyComponent() {
  const [count, setCount] = useState(0);

  useEffect(() => {
    // This side effect updates the document title
    document.title = `Count: ${count}`;
  }, [count]); // The effect re-runs whenever 'count' changes

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}

export default MyComponent;
```

# context hooks in react

<div data-processed="true" data-subtree="aimfl,mfl" id="bkmrk-context-hooks-in-rea" jscontroller="v48bt" jsuid="xKl5Ze_9" style="display: contents;">**Context hooks in React, primarily**</div><div data-processed="true" data-subtree="aimfl,mfl" id="bkmrk-" jscontroller="v48bt" jsuid="xKl5Ze_9" style="display: contents;"></div><div data-processed="true" data-subtree="aimfl,mfl" id="bkmrk--1" jscontroller="v48bt" jsuid="xKl5Ze_9" style="display: contents;"></div>[useContext](https://www.google.com/search?client=ubuntu-sn&channel=fs&q=useContext&mstk=AUtExfAunjEc3Jg-sEBzm2sxLB7-F02T1b7IdzbIMbqIgwAG5BT4cE3xlz_OaBqLs47LPh7O6PHUD_XrXKfMQnzeo4_WPWgLd3jb4xzH7TTTbV9gysdTmHDSV-6cpKC0nSgBsU-ad3U71jFeVpdvDXllUk8j2W2olPEzFWRHqlT2wJjHPilEFAfW8qNiFUc5O_4S9nGFfgBG660U_X2sFO0hTcRINN_GBcTvv21SHJihAI3W7DOa59l8nuHcNsD9_Jr4IZ-xsc7i42dyF3c51gsI1yrs&csui=3&ved=2ahUKEwjnu-_vnbCRAxWMUGwGHUPqPI0QgK4QegQIARAB), <mark class="HxTRcb" data-processed="true">allow functional components to **subscribe to and consume data from React's Context API**</mark>, simplifying global state management by avoiding "prop drilling" (passing props down many levels). You create a `Context` with `React.createContext()`, wrap parts of your app with a `Provider` to pass data down, and then use `useContext(MyContext)` in any nested component to read that data, making it ideal for themes, authentication, or user settings.<span class="uJ19be notranslate" data-processed="true" data-wiz-uids="xKl5Ze_h,xKl5Ze_i,xKl5Ze_j"><span class="vKEkVd" data-animation-atomic="" data-processed="true"> </span></span>

<div class="Y3BBE" data-hveid="CAEQAA" data-processed="true" data-sfc-cp="" id="bkmrk--2" jsaction="rcuQ6b:&xKl5Ze_8|npT2md" jscontroller="zcfIf" jsuid="xKl5Ze_8">  
</div><div class="Fsg96" data-processed="true" data-sfc-cp="" id="bkmrk--3" jsaction="rcuQ6b:&xKl5Ze_o|npT2md" jscontroller="KHhJQ" jsuid="xKl5Ze_o">  
</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk-key-components" jscontroller="a7qCn" jsuid="xKl5Ze_p" role="heading">**Key Components**</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk--4" jscontroller="a7qCn" jsuid="xKl5Ze_p" role="heading">  
</div>1. <span class="T286Pc" data-processed="true" data-sfc-cp="">**`React.createContext()`**: Creates a Context object, often with a default value, that components can subscribe to.</span>
2. <span class="T286Pc" data-processed="true" data-sfc-cp="">**`<Context.Provider>`**: A component that wraps a part of your component tree and uses the `value` prop to provide data to all descendants.</span>
3. <span class="T286Pc" data-processed="true" data-sfc-cp="">**`useContext(Context)`**: A hook used in functional components to read the current context value from the nearest `Provider` above it in the tree.</span><span class="uJ19be notranslate" data-processed="true" data-wiz-uids="xKl5Ze_18,xKl5Ze_19,xKl5Ze_1a"><span class="vKEkVd" data-animation-atomic="" data-processed="true"> </span></span>

<div class="Fsg96" data-processed="true" data-sfc-cp="" id="bkmrk--5" jsaction="rcuQ6b:&xKl5Ze_1h|npT2md" jscontroller="KHhJQ" jsuid="xKl5Ze_1h">  
</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk-how-it-works-%28simpli" jscontroller="a7qCn" jsuid="xKl5Ze_1i" role="heading">**How It Works (Simplified)**</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk--6" jscontroller="a7qCn" jsuid="xKl5Ze_1i" role="heading">  
</div>1. <span class="T286Pc" data-processed="true" data-sfc-cp="">**Create Context**:</span><div class="r1PmQe" data-hveid="CAUQAQ" data-processed="true" data-wiz-uids="xKl5Ze_1r,xKl5Ze_1s,xKl5Ze_1t" jscontroller="HP6Sjf" jsuid="xKl5Ze_1q"><div data-processed="true"><div class="pHpOfb" data-animation-atomic="" data-processed="true"><div class="vVRw1d" data-processed="true">javascript</div></div></div></div>
2. ```
    import { createContext } from 'react';
    const ThemeContext = createContext('light'); // Default value
    
    ```
3. <span class="T286Pc" data-processed="true" data-sfc-cp="">**Provide Context**: Wrap components with the `Provider` high up in the tree.</span><div class="r1PmQe" data-hveid="CAUQBA" data-processed="true" data-wiz-uids="xKl5Ze_21,xKl5Ze_22,xKl5Ze_23" jscontroller="HP6Sjf" jsuid="xKl5Ze_20"><div data-processed="true"><div class="pHpOfb" data-animation-atomic="" data-processed="true"><div class="vVRw1d" data-processed="true">javascript</div></div></div></div>
4. ```
    function App() {
      return (
        <ThemeContext.Provider value="dark">
          <Toolbar />
        </ThemeContext.Provider>
      );
    }
    
    ```
5. <span class="T286Pc" data-processed="true" data-sfc-cp="">**Consume Context**: Use `useContext` in any child component to get the value.</span><div class="r1PmQe" data-hveid="CAUQBw" data-processed="true" data-wiz-uids="xKl5Ze_2b,xKl5Ze_2c,xKl5Ze_2d" jscontroller="HP6Sjf" jsuid="xKl5Ze_2a"><div data-processed="true"><div class="pHpOfb" data-animation-atomic="" data-processed="true"><div class="vVRw1d" data-processed="true">javascript</div></div></div></div>

```
import { useContext } from 'react';
function Toolbar() {
  const theme = useContext(ThemeContext); // Gets "dark"
  return <div>The current theme is: {theme}</div>;
}

```

<span class="uJ19be notranslate" data-processed="true" data-wiz-uids="xKl5Ze_2g,xKl5Ze_2h,xKl5Ze_2i"><span class="vKEkVd" data-animation-atomic="" data-processed="true"> </span></span>

<div class="Fsg96" data-processed="true" data-sfc-cp="" id="bkmrk--7" jsaction="rcuQ6b:&xKl5Ze_2j|npT2md" jscontroller="KHhJQ" jsuid="xKl5Ze_2j">  
</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk-benefits" jscontroller="a7qCn" jsuid="xKl5Ze_2k" role="heading">**Benefits**</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk--8" jscontroller="a7qCn" jsuid="xKl5Ze_2k" role="heading">  
</div>- <span class="T286Pc" data-processed="true" data-sfc-cp="">**Avoids Prop Drilling**: Eliminates the need to pass props through intermediate components that don't need the data.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**Simpler Syntax**: More concise and declarative than older methods (like render props or Consumer components) for functional components.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**Global State**: Great for sharing data like themes, user info, or language settings across an application.</span><span class="uJ19be notranslate" data-processed="true" data-wiz-uids="xKl5Ze_2y,xKl5Ze_2z,xKl5Ze_30"><span class="vKEkVd" data-animation-atomic="" data-processed="true"> </span></span>

<div class="Fsg96" data-processed="true" data-sfc-cp="" id="bkmrk--9" jsaction="rcuQ6b:&xKl5Ze_35|npT2md" jscontroller="KHhJQ" jsuid="xKl5Ze_35">  
</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk-best-practice%3A-custo" jscontroller="a7qCn" jsuid="xKl5Ze_36" role="heading">**Best Practice: Custom Hooks**</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk--10" jscontroller="a7qCn" jsuid="xKl5Ze_36" role="heading">  
</div><div class="Y3BBE" data-hveid="CAkQAA" data-processed="true" data-sfc-cp="" id="bkmrk-for-cleaner-code%2C-es" jsaction="rcuQ6b:&xKl5Ze_3a|npT2md" jscontroller="zcfIf" jsuid="xKl5Ze_3a">For cleaner code, especially with complex contexts (like theme + setter), create a custom hook that encapsulates the `useContext` call and error handling (e.g., checking for null if used outside a provider).<span class="uJ19be notranslate" data-processed="true" data-wiz-uids="xKl5Ze_3c,xKl5Ze_3d,xKl5Ze_3e"><span class="vKEkVd" data-animation-atomic="" data-processed="true"> </span></span></div><div class="Y3BBE" data-hveid="CAkQAA" data-processed="true" data-sfc-cp="" id="bkmrk--11" jsaction="rcuQ6b:&xKl5Ze_3a|npT2md" jscontroller="zcfIf" jsuid="xKl5Ze_3a">  
</div><div class="Fsg96" data-processed="true" data-sfc-cp="" id="bkmrk--12" jsaction="rcuQ6b:&xKl5Ze_3g|npT2md" jscontroller="KHhJQ" jsuid="xKl5Ze_3g">  
</div><div class="r1PmQe" data-hveid="CAwQAA" data-processed="true" data-wiz-uids="xKl5Ze_7e,xKl5Ze_7f,xKl5Ze_7g" id="bkmrk-javascript" jscontroller="HP6Sjf" jsuid="xKl5Ze_7d"><div data-processed="true"><div class="pHpOfb" data-animation-atomic="" data-processed="true"><div class="vVRw1d" data-processed="true">javascript</div></div></div></div>```
// useTheme.js
import { useContext } from 'react';
import { ThemeContext } from './ThemeContext';

export function useTheme() {
  const context = useContext(ThemeContext);
  if (context === undefined) {
    throw new Error('useTheme must be used within a ThemeProvider');
  }
  return context;
}

```

# @types/react

`@types/react` is the <mark class="HxTRcb" data-processed="true">npm package containing TypeScript definitions for the React library</mark>, essential for building React apps with TypeScript to get type-checking for components, props, hooks (like `useState`, `useEffect`), and rendering types (like `ReactNode`). It provides the necessary structure for TypeScript to understand React's API, enabling features like autocompletion and catching errors before runtime, with types such as `React.FC` for functional components or `React.ReactNode` for renderable content.<span class="uJ19be notranslate" data-processed="true" data-wiz-uids="aAJ2Qe_l,aAJ2Qe_m,aAJ2Qe_n"><span class="vKEkVd" data-animation-atomic="" data-processed="true"> </span></span>

<div class="Y3BBE" data-hveid="CAEQAA" data-processed="true" data-sfc-cp="" id="bkmrk-" jsaction="rcuQ6b:&aAJ2Qe_8|npT2md" jscontroller="zcfIf" jsuid="aAJ2Qe_8">  
</div><div class="Fsg96" data-processed="true" data-sfc-cp="" id="bkmrk--1" jsaction="rcuQ6b:&aAJ2Qe_t|npT2md" jscontroller="KHhJQ" jsuid="aAJ2Qe_t">  
</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk-what-it-does%3A" jscontroller="a7qCn" jsuid="aAJ2Qe_u" role="heading">**What it does:**</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk--2" jscontroller="a7qCn" jsuid="aAJ2Qe_u" role="heading">  
</div>- <span class="T286Pc" data-processed="true" data-sfc-cp="">**Type Safety:** Gives TypeScript knowledge of React's functions, components, and props.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**Hooks Support:** Includes types for all built-in React hooks, allowing their correct usage.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**Component Definition:** Helps define component props, including `children` (often as `React.ReactNode`).</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**Rendering Types:** Defines types like `React.Element`, `React.ReactNode` (anything renderable), `JSX.Element`, etc..</span><span class="uJ19be notranslate" data-processed="true" data-wiz-uids="aAJ2Qe_1g,aAJ2Qe_1h,aAJ2Qe_1i"><span class="vKEkVd" data-animation-atomic="" data-processed="true"> </span></span>

<div class="Fsg96" data-processed="true" data-sfc-cp="" id="bkmrk--3" jsaction="rcuQ6b:&aAJ2Qe_1o|npT2md" jscontroller="KHhJQ" jsuid="aAJ2Qe_1o">  
</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk-how-to-use-it%3A" jscontroller="a7qCn" jsuid="aAJ2Qe_1p" role="heading">**How to use it:**</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk--4" jscontroller="a7qCn" jsuid="aAJ2Qe_1p" role="heading">  
</div>- <span class="T286Pc" data-processed="true" data-sfc-cp="">Install it as a development dependency: `npm install --save-dev @types/react` or `yarn add @types/react`.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">It's typically installed alongside `@types/react-dom` for full type support in your React+TypeScript projects.</span><span class="uJ19be notranslate" data-processed="true" data-wiz-uids="aAJ2Qe_21,aAJ2Qe_22,aAJ2Qe_23"><span class="vKEkVd" data-animation-atomic="" data-processed="true"> </span></span>

<div class="Fsg96" data-processed="true" data-sfc-cp="" id="bkmrk--5" jsaction="rcuQ6b:&aAJ2Qe_26|npT2md" jscontroller="KHhJQ" jsuid="aAJ2Qe_26">  
</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk-key-types-you%27ll-use" jscontroller="a7qCn" jsuid="aAJ2Qe_27" role="heading">**Key types you'll use:**</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk--6" jscontroller="a7qCn" jsuid="aAJ2Qe_27" role="heading">  
</div>- <span class="T286Pc" data-processed="true" data-sfc-cp="">**`React.FC<Props>`**: For functional components.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**`React.ReactNode`**: For props that accept anything renderable (children, content).</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**`React.HTMLAttributes<T>`**: For standard HTML attributes on components.</span><span class="uJ19be notranslate" data-processed="true" data-wiz-uids="aAJ2Qe_2o,aAJ2Qe_2p,aAJ2Qe_2q"><span class="vKEkVd" data-animation-atomic="" data-processed="true"> </span></span>

<div class="Fsg96" data-processed="true" data-sfc-cp="" id="bkmrk--7" jsaction="rcuQ6b:&aAJ2Qe_2u|npT2md" jscontroller="KHhJQ" jsuid="aAJ2Qe_2u">  
</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk-important-note%3A" jscontroller="a7qCn" jsuid="aAJ2Qe_2v" role="heading">**Important Note:**</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk--8" jscontroller="a7qCn" jsuid="aAJ2Qe_2v" role="heading">  
</div>- <span class="T286Pc" data-processed="true" data-sfc-cp="">Ensure your `@types/react` version matches your `react` version for consistency, as mismatches can cause issues (e.g., React 18 changes required corresponding type updates)</span>

# import React from 'react'

<div data-subtree="aimfl,mfl" id="bkmrk-import-react-from-%27r" jscontroller="v48bt" jsuid="jrFusd_9" style="display: contents;">`import React from 'react'`</div><mark class="HxTRcb">is a JavaScript statement to bring the React library into your file</mark>, historically required to use JSX (like `<div>`) because it converts to `React.createElement()`, though newer React versions with the "new JSX transform" (React 17+) often make it optional for basic JSX, but it's still needed for Hooks (like `useState`) and other exports, with `import * as React from 'react'` also common for importing everything into a `React` object.<span class="uJ19be notranslate" data-wiz-uids="jrFusd_h,jrFusd_i,jrFusd_j"><span class="vKEkVd" data-animation-atomic=""> </span></span>

<div class="Y3BBE" data-hveid="CAEQAA" data-processed="true" data-sfc-cp="" id="bkmrk-" jsaction="rcuQ6b:&jrFusd_8|npT2md" jscontroller="zcfIf" jsuid="jrFusd_8">  
</div><div class="Fsg96" data-processed="true" data-sfc-cp="" id="bkmrk--1" jsaction="rcuQ6b:&jrFusd_o|npT2md" jscontroller="KHhJQ" jsuid="jrFusd_o">  
</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk-what-it-does" jscontroller="a7qCn" jsuid="jrFusd_p" role="heading">**What it does**</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk--2" jscontroller="a7qCn" jsuid="jrFusd_p" role="heading">  
</div>- <span class="T286Pc" data-sfc-cp="">**Imports React:** It makes the `React` object, containing methods like `createElement`, available in your component.</span>
- <span class="T286Pc" data-sfc-cp="">**Enables JSX:** Before React 17, this line was essential because JSX syntax (e.g., `<p>Hello</p>`) gets transformed into `React.createElement(<p>, null, 'Hello')`.</span><span class="uJ19be notranslate" data-wiz-uids="jrFusd_14,jrFusd_15,jrFusd_16"><span class="vKEkVd" data-animation-atomic=""> </span></span>

<div class="Fsg96" data-processed="true" data-sfc-cp="" id="bkmrk--3" jsaction="rcuQ6b:&jrFusd_1a|npT2md" jscontroller="KHhJQ" jsuid="jrFusd_1a">  
</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk-modern-variations-an" jscontroller="a7qCn" jsuid="jrFusd_1b" role="heading">**Modern variations and when to use them**</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk--4" jscontroller="a7qCn" jsuid="jrFusd_1b" role="heading">  
</div>- <span class="T286Pc" data-sfc-cp="">**`import React from 'react'`:** Imports the default export (the whole library) and names it `React`. Still needed for Hooks.</span>
- <span class="T286Pc" data-sfc-cp="">**`import * as React from 'react'`:** Imports all named exports into a single `React` object. Useful in TypeScript or with specific bundler setups.</span>
- <span class="T286Pc" data-sfc-cp="">**No import (for simple components):** With React 17+, the new JSX transform handles basic JSX without needing the import, but you still need it for hooks.</span>
- <span class="T286Pc" data-sfc-cp="">**Specific imports:** For efficiency, import only what you need, e.g., `import { useState } from 'react'`.</span><span class="uJ19be notranslate" data-wiz-uids="jrFusd_1x,jrFusd_1y,jrFusd_1z"><span class="vKEkVd" data-animation-atomic=""> </span></span>

<div class="Fsg96" data-processed="true" data-sfc-cp="" id="bkmrk--5" jsaction="rcuQ6b:&jrFusd_25|npT2md" jscontroller="KHhJQ" jsuid="jrFusd_25">  
</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk-example-%28classic%29" jscontroller="a7qCn" jsuid="jrFusd_26" role="heading">**Example (Classic)**</div><div class="r1PmQe" data-hveid="CAkQAA" data-processed="true" data-wiz-uids="jrFusd_4m,jrFusd_4n,jrFusd_4o" id="bkmrk--6" jscontroller="HP6Sjf" jsuid="jrFusd_4l"><div><div class="pHpOfb" data-animation-atomic=""></div></div></div>```
import React from 'react'; // Required for React.useState

function Counter() {
  const [count, setCount] = React.useState(0); // Uses React.useState
  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}

```

<div class="Fsg96" data-processed="true" data-sfc-cp="" id="bkmrk--7" jsaction="rcuQ6b:&jrFusd_4r|npT2md" jscontroller="KHhJQ" jsuid="jrFusd_4r">  
</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk-example-%28modern---no" jscontroller="a7qCn" jsuid="jrFusd_4s" role="heading">**Example (Modern - no import needed for basic JSX)**</div><div class="r1PmQe" data-hveid="CAsQAA" data-processed="true" data-wiz-uids="jrFusd_4y,jrFusd_4z,jrFusd_50" id="bkmrk--8" jscontroller="HP6Sjf" jsuid="jrFusd_4x"><div data-processed="true"><div class="pHpOfb" data-animation-atomic="" data-processed="true"><div class="vVRw1d" data-processed="true">  
</div></div></div></div>```
// import React from 'react'; // Not strictly needed for just JSX

function Greeting({ name }) {
  // No React. prefix needed for useState with this setup
  return <h1>Hello, {name}!</h1>;
}

```

# createbrowserrouter in react

<div data-processed="true" data-subtree="aimfl,mfl" id="bkmrk-createbrowserrouter" jscontroller="v48bt" jsuid="SZleKb_9" style="display: contents;">`createBrowserRouter`</div>in React Router (v6.4+) <mark class="HxTRcb" data-processed="true">creates a router using a data-driven approach (array of route objects) instead of JSX</mark>, enabling powerful features like data loading (loaders) and mutations (actions), and is the recommended modern way for web apps by managing history via the HTML5 History API for clean URLs. You define routes as objects with `path` and `element` (or `children`) and pass them to `createBrowserRouter`, then wrap your app with the `RouterProvider` component.<span class="uJ19be notranslate" data-processed="true" data-wiz-uids="SZleKb_h,SZleKb_i,SZleKb_j"><span class="vKEkVd" data-animation-atomic="" data-processed="true"> </span></span>

<div class="Y3BBE" data-hveid="CAEQAA" data-processed="true" data-sfc-cp="" id="bkmrk-" jsaction="rcuQ6b:&SZleKb_8|npT2md" jscontroller="zcfIf" jsuid="SZleKb_8">  
</div><div class="Fsg96" data-processed="true" data-sfc-cp="" id="bkmrk--1" jsaction="rcuQ6b:&SZleKb_p|npT2md" jscontroller="KHhJQ" jsuid="SZleKb_p">  
</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk-how-to-use-createbro" jscontroller="a7qCn" jsuid="SZleKb_q" role="heading">**How to Use `createBrowserRouter`**</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk--2" jscontroller="a7qCn" jsuid="SZleKb_q" role="heading">  
</div>1. <span class="T286Pc" data-processed="true" data-sfc-cp="">**Install React Router:**</span><div class="r1PmQe" data-hveid="CAMQAQ" data-processed="true" data-wiz-uids="SZleKb_10,SZleKb_11,SZleKb_12" jscontroller="HP6Sjf" jsuid="SZleKb_z"><div data-processed="true"><div class="pHpOfb" data-animation-atomic="" data-processed="true"><div class="vVRw1d" data-processed="true">bash</div></div></div></div>
2. ```
    npm install react-router-dom
    # or
    yarn add react-router-dom
    
    ```
3. <span class="T286Pc" data-processed="true" data-sfc-cp="">**Define Your Routes (e.g., in `router.js` or `index.js`):**</span><div class="r1PmQe" data-hveid="CAMQBA" data-processed="true" data-wiz-uids="SZleKb_1b,SZleKb_1c,SZleKb_1d" jscontroller="HP6Sjf" jsuid="SZleKb_1a"><div data-processed="true"><div class="pHpOfb" data-animation-atomic="" data-processed="true"><div class="vVRw1d" data-processed="true">javascript</div></div></div></div>
4. ```
    import { createBrowserRouter } from 'react-router-dom';
    import Root from './routes/Root'; // Your main layout component
    import Home from './routes/Home';
    import About from './routes/About';
    import ErrorPage from './routes/ErrorPage';
    
    const router = createBrowserRouter([
      {
        path: "/",
        element: <Root />,
        errorElement: <ErrorPage />,
        children: [ // Nested routes
          { index: true, element: <Home /> }, // Renders <Home> at '/'
          { path: "about", element: <About /> },
          { path: "users/:userId", element: <UserProfile /> }, // Dynamic route
        ],
      },
    ]);
    
    export default router;
    
    ```
    
    
    - <span class="T286Pc" data-processed="true" data-sfc-cp="">**`path`**: The URL segment. `index: true` is for the default child route.</span>
    - <span class="T286Pc" data-processed="true" data-sfc-cp="">**`element`**: The component to render for that path.</span>
    - <span class="T286Pc" data-processed="true" data-sfc-cp="">**`children`**: For nested routes, uses `<Outlet />` in the parent.</span>
5. <span class="T286Pc" data-processed="true" data-sfc-cp="">**Provide the Router (in `index.js` or `App.js`):**</span><div class="r1PmQe" data-hveid="CAMQCg" data-processed="true" data-wiz-uids="SZleKb_21,SZleKb_22,SZleKb_23" jscontroller="HP6Sjf" jsuid="SZleKb_20"><div data-processed="true"><div class="pHpOfb" data-animation-atomic="" data-processed="true"><div class="vVRw1d" data-processed="true">javascript</div></div></div></div>

```
import React from 'react';
import ReactDOM from 'react-dom/client';
import { RouterProvider } from 'react-router-dom';
import router from './router'; // Your router config

ReactDOM.createRoot(document.getElementById('root')).render(
  <React.StrictMode>
    <RouterProvider router={router} />
  </React.StrictMode>,
);

```

<span class="uJ19be notranslate" data-processed="true" data-wiz-uids="SZleKb_26,SZleKb_27,SZleKb_28"><span class="vKEkVd" data-animation-atomic="" data-processed="true"> </span></span>

<div class="Fsg96" data-processed="true" data-sfc-cp="" id="bkmrk--3" jsaction="rcuQ6b:&SZleKb_2c|npT2md" jscontroller="KHhJQ" jsuid="SZleKb_2c">  
</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk-key-advantages-%28vs.-" jscontroller="a7qCn" jsuid="SZleKb_2d" role="heading">**Key Advantages (vs. `<BrowserRouter>`/JSX Routes)**</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk--4" jscontroller="a7qCn" jsuid="SZleKb_2d" role="heading">  
</div>- <span class="T286Pc" data-processed="true" data-sfc-cp="">**Data Layer:** Supports `loader` functions for fetching data before rendering, ensuring components get data immediately.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**Data Mutations:** Supports `action` functions for POST/PUT/DELETE requests, improving data flow.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**Flexibility:** Configure routes as JS objects (data) instead of JSX, separating concerns.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**Future-Proof:** Recommended by React Router as the modern direction</span>

# useMemo,useLocation from react

<div class="WaaZC" id="bkmrk-usememo-and-uselocat"><div class="RJPOee EIJn2" style="animation: none !important;"><div class="rPeykc" data-hveid="CAUQAQ" data-ved="2ahUKEwjYoKjForCRAxVDR2wGHdsBJzUQo_EKegQIBRAB"><span data-huuid="9465597073531100809">`useMemo` and `useLocation` are two distinct hooks in the React ecosystem, serving different purposes. </span></div></div></div><div class="rPeykc" data-hveid="CAUQAQ" data-ved="2ahUKEwjYoKjForCRAxVDR2wGHdsBJzUQo_EKegQIBRAB" id="bkmrk-">  
</div><div class="rPeykc" data-hveid="CAUQAQ" data-ved="2ahUKEwjYoKjForCRAxVDR2wGHdsBJzUQo_EKegQIBRAB" id="bkmrk--1"></div><div class="WaaZC" id="bkmrk-usememo-%28from-react%29"><div class="RJPOee EIJn2" style="animation: none !important;"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CAIQAQ" data-ved="2ahUKEwjYoKjForCRAxVDR2wGHdsBJzUQo_EKegQIAhAB" role="heading">**<span data-huuid="9465597073531098711">`useMemo` (from `react`) </span>**</div></div></div><div class="WaaZC" id="bkmrk-usememo-is-a-react-h"><div class="RJPOee EIJn2" style="animation: none !important;"><div class="rPeykc" data-hveid="CAkQAQ" data-ved="2ahUKEwjYoKjForCRAxVDR2wGHdsBJzUQo_EKegQICRAB"><span data-huuid="9465597073531100709">`useMemo` is a React Hook that allows for memoization of a value. </span><span data-huuid="9465597073531099660">This means it caches the result of an expensive calculation and only re-runs the calculation if its dependencies change. </span><span data-huuid="9465597073531098611">This can significantly improve performance by preventing unnecessary re-calculations on every render. </span></div></div></div><div class="WaaZC" id="bkmrk--2"><div class="RJPOee EIJn2" style="animation: none !important;"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc"><div class="zYSUYd"><div class="FS7GEb"><div class="dDrxod"><div aria-live="polite" class="WDoJJe">  
</div></div></div></div></div></div></div></div>```
import React, { useMemo } from 'react';

function MyComponent({ data }) {
  // Expensive calculation that only re-runs if 'data' changes
  const processedData = useMemo(() => {
    // Perform complex data processing here
    return data.map(item => item.value * 2);
  }, [data]); // Dependencies array

  return (
    <div>
      {/* Render processedData */}
      {processedData.map((item, index) => (
        <p key={index}>{item}</p>
      ))}
    </div>
  );
}
```

<div aria-level="3" class="rPeykc pyPiTc" data-hveid="CBAQAQ" data-ved="2ahUKEwjYoKjForCRAxVDR2wGHdsBJzUQo_EKegQIEBAB" id="bkmrk--3" role="heading">  
</div><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CBAQAQ" data-ved="2ahUKEwjYoKjForCRAxVDR2wGHdsBJzUQo_EKegQIEBAB" id="bkmrk--4" role="heading"></div><div class="WaaZC" id="bkmrk-uselocation-%28from-re"><div class="RJPOee EIJn2" style="animation: none !important;"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CBAQAQ" data-ved="2ahUKEwjYoKjForCRAxVDR2wGHdsBJzUQo_EKegQIEBAB" role="heading"><span data-huuid="9404912379262324393">**`useLocation` (from `react-router-dom`)** </span></div><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CBAQAQ" data-ved="2ahUKEwjYoKjForCRAxVDR2wGHdsBJzUQo_EKegQIEBAB" role="heading">  
</div></div></div><div class="WaaZC" id="bkmrk-uselocation-is-a-hoo"><div class="RJPOee EIJn2" style="animation: none !important;"><div class="rPeykc" data-hveid="CBIQAQ" data-ved="2ahUKEwjYoKjForCRAxVDR2wGHdsBJzUQo_EKegQIEhAB"><span data-huuid="9404912379262323967">`useLocation` is a hook provided by the `react-router-dom` library, not directly from `react`. </span><span data-huuid="9404912379262321706">It allows you to access the current URL's `location` object, which contains information about the current route. </span><span data-huuid="9404912379262323541">This information includes `pathname`, `search` (query parameters), `hash`, and `state` (data passed during navigation). </span></div><div class="rPeykc" data-hveid="CBIQAQ" data-ved="2ahUKEwjYoKjForCRAxVDR2wGHdsBJzUQo_EKegQIEhAB">  
</div></div></div>```
import React from 'react';
import { useLocation } from 'react-router-dom';

function CurrentRouteInfo() {
  const location = useLocation();

  return (
    <div>
      <p>Pathname: {location.pathname}</p>
      <p>Search Query: {location.search}</p>
      <p>Hash: {location.hash}</p>
      {/* Access state data if available */}
      {location.state && <p>State Data: {JSON.stringify(location.state)}</p>}
    </div>
  );
}
```

<div class="WaaZC" id="bkmrk-key-differences%3A"><div class="RJPOee EIJn2" style="animation: none !important;"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CBsQAQ" data-ved="2ahUKEwjYoKjForCRAxVDR2wGHdsBJzUQo_EKegQIGxAB" role="heading"></div><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CBsQAQ" data-ved="2ahUKEwjYoKjForCRAxVDR2wGHdsBJzUQo_EKegQIGxAB" role="heading"></div><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CBsQAQ" data-ved="2ahUKEwjYoKjForCRAxVDR2wGHdsBJzUQo_EKegQIGxAB" role="heading">**<span data-huuid="5209593288003989533">Key Differences: </span>**</div></div></div><div class="WaaZC" id="bkmrk-origin%3A-usememo-is-a"><div class="RJPOee EIJn2" style="animation: none !important;">- <div class="zMgcWd dSKvsb" data-il=""><div data-crb-p=""><div class="xFTqob"><div class="Gur8Ad" style="display: inline;"><span data-huuid="5209593288003991088">**Origin:** </span></div><div class="vM0jzc" style="display: inline;"><span data-huuid="5209593288003990241">`useMemo` is a core React hook, while `useLocation` is part of `react-router-dom`. </span></div></div></div></div>
- <div class="zMgcWd dSKvsb" data-il=""><div data-crb-p=""><div class="xFTqob"><div class="Gur8Ad" style="display: inline;"><span data-huuid="5209593288003992643">**Purpose:** </span></div><div class="vM0jzc" style="display: inline;"><span data-huuid="5209593288003991796">`useMemo` optimizes performance by memoizing values, while `useLocation` provides access to route information. </span></div></div></div></div>
- <div class="zMgcWd dSKvsb" data-il=""><div data-crb-p=""><div class="xFTqob"><div class="Gur8Ad" style="display: inline;"><span data-huuid="5209593288003990102">**Dependencies:** </span></div><div class="vM0jzc" style="display: inline;"><span data-huuid="5209593288003993351">`useMemo` takes a dependencies array to determine when to re-calculate, while `useLocation` automatically updates when the URL changes</span></div></div></div></div>

</div></div>