How to create and provide context

Developed with

The course is part of this learning path

ReactJS: Zero to Hero
Start course


This module looks at how to manage state in React. You’ll be looking at state management, context, and reducers.  

Learning Objectives 

The objectives of this module are to provide you with an understanding of: 

  • State Management  
  • Context  
  • How to create and provide context  
  • Reducers  
  • How to provide dispatch 

Intended Audience  

This learning path is aimed at all who wish to learn how to use the ReactJS framework.  


It is essential you understand the face of contemporary web development to attend this course. We insist upon JavaScript experience, along with good HTML and CSS skills. 


We welcome all feedback and suggestions - please contact us at to let us know what you think. 


So how do we create and provide context in our application? To abstract state management away from components, it's usual to create a new JavaScript file to hold the context in. The createContext functions from the React library creates a context object. It usually takes in an argument. This unless a component doesn't have a matching provider above it in the tree, in which case a default value can be used. This is useful when testing components. A context provider that wraps any child components in the context is made available as a React component. When React renders components that subscribe to the context object, it finds the closest matching provider above it in the tree, and reads the context value it's holding. So, in the example, shown, if any of the components B, C, D, E, or F want to use MyContext, they will find the MyContext provider above it in the tree, and access whatever value it holds. FilterableCoursesTable app isn't really a use case for context, but we can use it to show you the mechanics of how it works. For this, what we want to do is abstract the getting and setting of the courses, from the FilterableCoursesTable component. It may be that we want to use the same courses data across a number of other trees we plan to add to the app. We'll provide the courses context above the FilterableCoursesTable in the tree, and then consume the data wherever we need to. An advantage of this is that if we find we add a component in the tree lower down, later in development, and realize it needs to use the context value, we don't have to modify every component above it. Just consume the context instead. In the top code window, you can see that we have created a CoursesProvider file, in a StateManagement folder. Inside this, we need to import React, and create context. We declare CoursesContext, which is set to the return of createContext function. This creates the context object. We then create a CoursesProvider, which is just a React component. For the time being, we'll go back to using static data, and populate value with the imported courses data. This provider doesn't know what its children will be, so we receive them as props on rendering. Children will be a component tree generated by React, from the actual child components you place inside the provider. Using components in this way is called composition, and it's a standard pattern used in React. In the code in the bottom window, you can see that we have surrounded the routes that render the FilterableCoursesTable in the app component, in a CoursesProvider component. With all this running, we can see that the React dev tools report that the CoursesContext provider, in the component hierarchy in the app, has a props of children, which are the route nodes that are rendered inside it, and the value, which is an object with a key called courses, and a value of our array of course objects. We now need to know how we can access this data from the provider and its children.


Introduction to State Management - Context - Consume Context - Reducers - Actions in Reducers - The useReducer Hook - Providing Dispatch

About the Author
Learning paths6

An outstanding trainer in software development with more than 15 years experience as a Corporate and Apprentice Trainer, ICT Teacher and Head of Department, with a passion for technology and its uses. Continuing to develop existing and new skills and courses, primarily in web design using PHP, JavaScript, HTML, CSS and SQL but also OOP (Java), programming foundations (Using Python), DevOps (Git, CI/CD, etc) and Agile/Scrum. Practically minded, a quick learner and a problem solver with an attention to detail to ensure high quality outcomes.

Covered Topics