# 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;
}

```