2.2.3: useContext

Learning Objectives

  1. React context allows us to access shared state from our components without passing props

  2. We should use context sparingly, only for state that would be painful to share through passing props

  3. How to use useContext to simplify syntax when using context with functional components

Introduction

React context allows us to share state across our app without passing it as props. This is helpful for apps with many levels of component nesting when we would need to use the same state across multiple components and pass state as props through many levels of components (aka "prop drilling"). The React team recommends we use context sparingly because it makes component reuse more difficult.

"Context is designed to share data that can be considered “global” for a tree of React components, such as the current authenticated user, theme, or preferred language." - React official docs on context

"...sometimes the same data needs to be accessible by many components in the tree, and at different nesting levels. Context lets you “broadcast” such data, and changes to it, to all components below. Common examples where using context might be simpler than the alternatives include managing the current locale, theme, or a data cache." - React official docs on context

Please read the following official React guide on context.

React Context
  1. Note the Create Context example, at this point the default value is being paseed, in this case 1. It refers to the largest heading level in this example but any value can be passed, even an object.

  2. Note the Use Context example, after developing a context you can use the information within by utilising the useContext hook.

  3. Note the Provide the Context example, to give Components access to the generated context, you must create a Context Provider to encapsulate all of the Components that require this information.

  4. Checkout this full example here and breakdown how to implement and use context within a React application to avoid prop drilling and repeatative code.

useContext

useContext is a React Hook that allows us to retrieve the latest value of the context object passed to it. Rocket recommends using useContext for context when using functional components.

Please read the following official React guide on useContext.

React useContext Hook
  1. Besides providing simpler syntax for reading context, useContext does not change the use cases for context. The examples from the official guide on context still apply, albeit with different syntax.

Sample Implementation example (Legacy CRA setup)

React Hooks useContext

Please checkout this repository for an example implementation of React useContext, ensure that you're on the main branch if you want to test out the application on your machine you will need to install the dependencies with the command npm install after the installation you can then run the application with the command npm run dev. Note that the video's code is similar but not the same as the given repository.

Checkout the files App.jsx, to see the how to create context using the createContext method, implemented on line 20. The user object is also defined in the App.jsx and is passed into the UserContext.Provider, this file also showcases how to share this context information by wrapping around the RouterProvider so that all children (wrapped components) can share the user data.

Within the Profile.jsx we can see how to use the user information that is shared within the Applications context, it is required that you import the UserContext that was defined in the App.jsx as well as the useContext from React. Then utilise the useContext method passing in the requested context, in this case, the UserContext. Then you can access the information as your would a JavaScript object inside the Components JSX.

Last updated