How to create and provide context

The course is part of this learning path

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. 


- [Instructor] 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 job script file to hold the context in. To create context functions from the React library create a context object. It usually takes no argument. This and lesser component doesn't have a much in provider above it in the tree. In which case the default value can be used. This is useful when testing components. A ContextProvider, 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 match in 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, wants to use MyContext, they will find the myContext provider above it in the tree, and access whatever value it holds. FilterableCoursesTable isn't really used 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 FilterablCoursesTable 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've created a CoursesProvider file in a state management 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 it received them as props on rendering. Children will be a componentry generated by React from the actual child components she placed 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 roots that render the FilterableCoursesTable in the up component in a CoursesProvider component. With all this running, we can see that the React dev tools report that the courses ContextProvider in the component hierarchy in the app has a props of children which are the root nodes that are rendered inside it. And the value which is an object with a key core 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 Paths

Ed is an Outstanding Trainer in Software Development, with a passion for technology and its uses and holding more than 10 years’ experience.

Previous roles have included being a Delivery Manager, Trainer, ICT teacher, and Head of Department. Ed continues to develop existing and new courses, primarily in web design using: PHP, JavaScript, HTML, CSS, SQL, and OOP (Java), Programming Foundations (Python), and DevOps (Git, CI/CD, etc). Ed describes himself as practically minded, a quick learner, and a problem solver who pays great attention to detail. 

Ed’s specialist area is training in Emerging Technologies, within Web Development. Ed mainly delivers courses in JavaScript covering vanilla JS, ES2015+, TypeScript, Angular, and React (the latter is authored by Ed) and has delivered on behalf of Google for PWAs. Ed has also developed a new suite of PHP courses and has extensive experience with HTML/CSS and MySQL. 

Ed is responsible for delivering QA’s Programming Foundations course using the Eclipse IDE. His skillset extends into the DevOps sphere, where he is able to deliver courses based around Agile/Scrum practices, version control, and CI/CD.

Covered Topics