Demystifying React Context

A guide to understanding and using React Context.

Cover Image

Introduction to React Context

React Context provides a way to pass data through the component tree without having to pass props down manually at every level. In this blog post, we will explore how to use React Context to manage state and share data across your application.

Creating a Context

To create a context, use the createContext function from the React library.

import React, { createContext } from "react";
 
const MyContext = createContext();

Providing Context

To make the context available to components, wrap them with the Provider component and pass the value you want to share.

import React, { createContext } from "react";
 
const MyContext = createContext();
 
function App() {
  return (
    <MyContext.Provider value={{ data: "Hello, World!" }}>
      <MyComponent />
    </MyContext.Provider>
  );
}

Consuming Context

To consume the context, use the useContext hook or the Consumer component.

Using useContext Hook

import React, { useContext } from "react";
import { MyContext } from "./MyContext";
 
function MyComponent() {
  const context = useContext(MyContext);
  return <div>{context.data}</div>;
}

Using Consumer Component

import React from "react";
import { MyContext } from "./MyContext";
 
function MyComponent() {
  return (
    <MyContext.Consumer>
      {(context) => <div>{context.data}</div>}
    </MyContext.Consumer>
  );
}

Updating Context

To update the context value, you can pass a function to the Provider and call it from a consumer component.

import React, { createContext, useState } from "react";
 
const MyContext = createContext();
 
function App() {
  const [data, setData] = useState("Hello, World!");
 
  return (
    <MyContext.Provider value={{ data, setData }}>
      <MyComponent />
    </MyContext.Provider>
  );
}
 
function MyComponent() {
  const { data, setData } = useContext(MyContext);
 
  return (
    <div>
      <p>{data}</p>
      <button onClick={() => setData("Hello, React Context!")}>Update</button>
    </div>
  );
}

Conclusion

React Context is a powerful tool for managing state and sharing data across your application. By understanding how to create, provide, consume, and update context, you can simplify your component tree and avoid prop drilling.

Happy coding!

Suggested Articles

Cover Image
New Next Blog

Welcome to my second blog post! This is a page created using MDX.

Cover Image
Next Blog

Welcome to my second blog post! This is a page created using MDX.

Cover Image
Demystifying React Context

A guide to understanding and using React Context.

Upskill Your Frontend Development Techniques 🌟

Subscribe to stay up-to-date and receive quality frontend development tutorials straight to your inbox!

No spam, sales, or ads. Unsubscribe anytime you wish.