ReactJS Foundations

Listing 11-9: Using Context with the useContext Hook

Previous Listing | Next Listing

Welcome to my app.

import React from 'react';
import { ThemeContext } from './theme-context'

function App() {
  const { theme } = React.useContext(ThemeContext)
  return (
    <>
      <header
        className="App-header"
        style={{ backgroundColor: theme.backgroundColor, color: theme.color }}
      >
        <h1>Welcome to my app.</h1>
      </header>
    </>
  )
}

export default App;

Download the examples, report issues, and ask/answer questions in the discussion area by visiting the book's github page. All of the code for the book is also available on codesandbox.io for you to play around with.

ReactJS Foundations is published by John Wiley and Sons, Inc and is available in paperback and eBook.

React JS Foundations

Loading...