DEV Community

Cover image for React Hooks: useContext
Cindy Hernandez
Cindy Hernandez

Posted on

React Hooks: useContext

If you are learning React, you have probably come across something called Hooks. One of the many useful hooks is
useContext.


What Is useContext?

This is a way to share values between components without having to pass a prop through every level of the tree. Instead, we define it on a global level and it can be accessed in any child component.


We begin by creating an AppContext component where we will set all global states.

AppContext


We need need to make sure that we wrap all the global state variables around a

AppContext


Once the AppContext component is created we can then go into our index component, import AppProvider, and wrap our with

index


Once all of that is completed we can start using those global states in our components.

LiftProgress Component


When Should I Use useContext?

useContext is great when you have some data that many different components need to access. It saves you from having to pass that data around through props, which can get confusing and messy.

Top comments (0)