Created
August 27, 2019 11:13
-
-
Save zachsa/f59b331f9e749822b15a423a397c9e80 to your computer and use it in GitHub Desktop.
The simplest POC I can think of to use the React Context API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react' | |
import { render } from 'react-dom' | |
import Hello from './Hello' | |
import './style.css' | |
const AppContext = React.createContext('app') | |
const Heading2 = props => ( | |
<AppContext.Consumer> | |
{ context => ( | |
<h1 style={context}>I'm Heading2</h1> | |
)} | |
</AppContext.Consumer> | |
) | |
class Heading1 extends Component { | |
static contextType = AppContext | |
render() { | |
return <h1 style={this.context}>I'm Heading1</h1> | |
} | |
} | |
const Content = props => ( | |
<> | |
<Heading1 /> | |
<Heading2 /> | |
</> | |
) | |
class App extends Component { | |
render() { | |
return ( | |
<AppContext.Provider value={{color: 'blue', backgroundColor: 'red'}}> | |
<Content /> | |
</AppContext.Provider> | |
) | |
} | |
} | |
const Index = () => <App /> | |
render(<Index />, document.getElementById('root')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment