Skip to content

Instantly share code, notes, and snippets.

@zachsa
Created August 27, 2019 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zachsa/f59b331f9e749822b15a423a397c9e80 to your computer and use it in GitHub Desktop.
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
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