Moving Context Provider to its Own Component

Moving Context Provider to its Own Component

We move the <Provider> to its own component. This will eventually allow us to add state and a toggle theme to it and hook up our button!

import React, {Component} from "react"
import {ThemeContextConsumer} from "./themeContext"

function Header(props) {
    return (
        // Consuming Context
        <ThemeContextConsumer>
            {theme => (
                <header className={`${theme}-theme`}>
                    <h2>{theme === "light" ? "Light" : "Dark"} Theme</h2>
                </header>
            )}
        </ThemeContextConsumer>
    )    
}

export default Header

Last updated

Was this helpful?