Author -  Sai gowtham

How to use React useContext hook to consume data

In this tutorial, we will learn about a better way to consume the context data by using useContext hook.

React Hooks tutorial series

What is React Context API?

React context API helps us to pass the data down to the react component tree instead of manually passing the data to every component by using props.

Let’s see a context API example.

import React from "react";

// ThemeContext is created with default value "null"
const ThemeContext = React.createContext(null);
// it returns two components Provider and Consumer

function App() {
  return (
    <div>
      {/* we are wrapping the Provider component by passing
      the current value "dark" */}
      <ThemeContext.Provider value="dark">
        <Post />
      </ThemeContext.Provider>
    </div>
  );
}

// Now we are consuming the ThemeContext data inside Post component
function Post() {
  return (
    <ThemeContext.Consumer>
      {theme => (
        <div className={theme}>
          {console.log(theme) // dark}
          <h1>My posts</h1>
        </div>
      )}
    </ThemeContext.Consumer>
  );
}

In the above code first, we created a Context bypassing the initial value null it returns back two components Provider and Consumer.

Provider : It is used to pass the data down to the components.

Consumer : It is used to consume the data which is passed by the Provider component.

Inside the Consumer Component, we need to use the function as a child so that we can access the data in the function parameter.

useContext hook usage example

Let’s rewrite the above code by using the useContext hook.

import React,{useContext} from "react";

// ThemeContext is created with default value "null"
const ThemeContext = React.createContext(null);
// it returns two components Provider and Consumer

function App() {
  return (
    <div>
      {/* we are wrapping the Provider component by passing
      the current value "dark" */}
      <ThemeContext.Provider value="dark">
        <Post />
      </ThemeContext.Provider>
    </div>
  );
}

// Now we are consuming the ThemeContext data inside Post component
function Post() {
 const theme = useContext(ThemeContext);

  return (
        <div className={theme}>
          {console.log(theme)}
            <h1>My posts</h1>
        </div>
  );
}

Have you seen the magic In Post component instead of Wrapping with Provider Component we just invoke the useContext hook by passing the ThemeContext,So that we can access the data which is passed by using value prop in the Provider component.

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY