useContext

Learn how to use the useContext Hook in functional components.

The useContext Hook is used to manage states globally. It helps avoid prop drilling. For example, if we have to send the props from the parent component to a child component that is deeply nested, we have to pass the props all the way down from the parent to the child. During this process, the props will also have to pass through the components that are between the parents and the child. This is unnecessary because the components in between have to take in unrelated data. This is where the useContext Hook comes in. It allows us to share the state between deeply nested components without prop drilling.

Usage

To implement and use the useContext Hook, we first have to import it from the react library along with createContext.

Get hands-on with 1200+ tech skills courses.