Skip to main content
InterviewHubPRO
Section Overview
Q392

Question 392 of 407

Part 9: React Hooks Deep Dive (262 - 316)

96%
React Core & Hooks (407 Qs)•Part 9: React Hooks Deep Dive (262 - 316)

Give an example on How to use context?

mid LevelCode ExampleHooks

Context is designed to share data that can be considered global for a tree of React components.

css
For example, in the code below lets manually thread through a “theme” prop in order to style the Button component.
javascript
    //Lets create a context with a default theme value "luna"
    const ThemeContext = React.createContext("luna");
    // Create App component where it uses provider to pass theme value in the tree
    class App extends React.Component {
      render() {
        return (
          <ThemeContext.Provider value="nova">
            <Toolbar />
          </ThemeContext.Provider>
        );
      }
    }
    // A middle component where you don't need to pass theme prop anymore
    function Toolbar(props) {
      return (
        <div>
          <ThemedButton />
        </div>
      );
    }
    // Lets read theme value in the button component to use
    class ThemedButton extends React.Component {
      static contextType = ThemeContext;
      render() {
        return <Button theme={this.context} />;
      }
    }