DEV Community

Mustafa Onur Çelik
Mustafa Onur Çelik

Posted on

2 2 1 1

How to use useContext hook in ReactJS

In React, useContext is a hook that allows you to access the value of a context object from within a functional component. Context provides a way to pass data through the component tree without having to pass props down manually at every level. This can be useful for sharing data that is considered "global" for a tree of components, such as the current authenticated user, theme, or language.

To use useContext, you first need to create a context object using the React.createContext method. This method takes a default value as an argument, which will be used when a component does not have a matching Provider above it in the tree. Here is an example of how you might create a context object for a theme:

const ThemeContext = React.createContext('light');
Enter fullscreen mode Exit fullscreen mode

Next, you can use the component to provide a value for the context object within a component tree. This component takes a value prop that will be used as the value for the context. Here is an example of how you might provide a theme value within a component:

const MyApp = () => {
  const [theme, setTheme] = useState('light');

  return (
    <ThemeContext.Provider value={theme}>
      {/* The rest of the app goes here */}
    </ThemeContext.Provider>
  );
}
Enter fullscreen mode Exit fullscreen mode

Finally, you can use the useContext hook to access the value of the context object from within a functional component. This hook takes the context object as an argument and returns the current context value for that context. Here is an example of how you might use useContext to access the theme value in a child component:

const MyChildComponent = () => {
  // Call useContext and pass it the context object
  const theme = useContext(ThemeContext);

  return <div>The current theme is: {theme}</div>;
}
Enter fullscreen mode Exit fullscreen mode

In this example, useContext will return the current value of the ThemeContext object, which is provided by the nearest component above it in the tree. This allows the child component to access the theme value without having to pass it down as a prop.

It's important to note that the value of a context object will only update if a component higher up in the tree calls setContext (for context created with useReducer) or re-renders with a new value prop (for context created with createContext). This means that if you want to update the value of a context object from a child component, you will need to pass a callback down as a prop that calls setContext or re-renders the parent component with a new value prop.

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (1)

Collapse
 
lalami profile image
Salah Eddine Lalami

@ IDURAR , we use react context api for all UI parts , and we keep our data layer inside redux .

Here Article about : 🚀 Mastering Advanced Complex React useContext with useReducer ⭐ (Redux like Style) ⭐ : dev.to/idurar/mastering-advanced-c...


Mastering Advanced Complex React useContext with useReducer (Redux like Style)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️