ReactJS – useContext Hook

Page Content:
3.React Context API


1.In the class component, Context API is used to set the global data and this data can now be accessed in any of the child components without passing them through every parent Component(Through props).
2.useContext is  better replacement of Context APIs(Consumer & contextType) of class component.


const value = useContext(MyContext);

    useContext Accepts the value returned from React.createContext and returns the current context value for that context.
    When the nearest <MyContext.Provider> above the component updates, this hook will trigger a re-render with the latest context value passed to that MyContext provider.

3.React Context API:

1.The React Context API allows us to share data across all levels of the application.
2.The main aim of Context API is to solve the problem of prop drilling (also called “Threading”).

The Context APIs in React are given below.

3.Context.Consumer //Replaced by useContext
4.Class.contextType //Replaced by useContext

1.useContext(MyContext) is equivalent to static contextType = MyContext or <MyContext.Consumer> in a class component.Hence we wont be discussing Context.Consumer & Class.contextType .
2.useContext(MyContext) only lets you read the context and subscribe to its changes. You still need a <MyContext.Provider> above in the tree to provide the value for this context.


    createContext Creates a Context object. When React renders a component that uses this Context object, it will read the current context value from the closest matching Provider above it in the tree.


const MyContext = React.createContext(defaultValue); 


1.Every Context object comes with a Provider React component that allows providing the context object on context change.
2.The Provider component accepts a value prop to be passed to consuming components that are descendants of this Provider.
3.All consumers that are descendants of a Provider will re-render whenever the Provider’s value prop changes.

<ThemeContext.Provider value={themes.dark}>
      <Toolbar />

c)Use-Case for useConext with function component:

    Provides the context object for both context provider & consumer(child component which uses context)

import React from 'react';

const ThemeContext = React.createContext();

export default ThemeContext;

    Context Provider for Toolbar Component

import React ,{ useState }from 'react';
import ThemeContext from'./ThemeContext';
import Toolbar from './Toolbar';
function App() {
   const themes = {
  light: {
    foreground: "#000000",
    background: "#eeeeee"
  dark: {
    foreground: "#ffffff",
    background: "#222222"
var [theme ,setTheme] = useState(themes.light);

  return (
    <ThemeContext.Provider value={theme}>
      <Toolbar />
       <button onClick={()=>{setTheme(themes.light)}}>Light Theme</button>
       <button onClick={()=>{setTheme(themes.dark)}} >Dark Theme</button>
export default App;

    Context consuming in ThemedButton Component using useContext Hook.

import React ,{useContext} from 'react';
import ThemeContext from './ThemeContext';

function Toolbar(props) {
  return (
      <ThemedButton />

  function ThemedButton() {
  const theme = useContext(ThemeContext);
  return (
    <button style={{ background: theme.background, color: theme.foreground }}>
      I am styled by theme context!

export default Toolbar;

Final Result :

Leave a Reply