React Context

Learn how to create and use React context.

A React application consists of components that can receive props passed from the parent. As we know, props flow from the parent component down to a child component. React context can be used to save a state value and make it available and accessible to all components without passing it down as props. React context creates a general location where data can be placed and retrieved.

Creating a context

Open the context.js file. On line 3, a context is created by using React.createContext. On line 5, a function named AppProvider is defined. Inside this function on line 6, we declare a state variable called data and a setData function that will change the data state variable. The created context stores a string ...