How to create and provide context
The course is part of this learning path
This module looks at how to manage state in React. You’ll be looking at state management, context, and reducers.
The objectives of this module are to provide you with an understanding of:
- State Management
- How to create and provide context
- How to provide dispatch
This learning path is aimed at all who wish to learn how to use the ReactJS framework.
We welcome all feedback and suggestions - please contact us at firstname.lastname@example.org 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.
Ed is an Outstanding Trainer in Software Development, with a passion for technology and its uses and holding more than 10 years’ experience.
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.