Skip to content Skip to sidebar Skip to footer

React Context Provider And Consumer

ThemeContext.js import React from 'react'; const ThemeContext = React.createContext(null); export default ThemeContext; Parent component import ThemeContext from './ThemeContext

Solution 1:

It is the relationship between Provider parent and Consumer descendants that allows to share values between them.

<ThemeContext.Consumer> in C has <ThemeContext.Provider value={'green'}> as parent, so it's able to access green context value as coloredTheme callback parameter.

both have their own ThemeContext?

ES modules are evaluated once and result in singletons. ThemeContext is context object. It is same in A and C modules.

Post a Comment for "React Context Provider And Consumer"