React component that handles React context Provider and Consumer for use with context API in React v.16.3.0, and especially v16.6.0 with contextType allowing way to subscribe to context from a class
npm install --save @stahlmandesign/rc-state-context
import { StateProvider } from '@stahlmandesign/rc-state-contenxt'
import { StateConsumer } from '@stahlmandesign/rc-state-contenxt'
import React from 'react'
import { StateProvider } from 'StateContext'
class App extends React.Component{
state = { data: "I'm a string stored in App.state.data" }
render(){
return (
<div className='App'>
<StateProvider state={ this.state } setState={ this.setState.bind(this) }>
{/* NOTE all your other components here including routes etc. */}
{/* any child component can import StateConsumer */}
{/* and access the state and setState of the main App */}
<ExampleChildComponent/>
</StateProvider>
</div>
)
}
}
export default App
import React from 'react'
import { StateConsumer } from 'StateContext'
class ExampleChildCompoment extends React.Component {
static contextType = StateConsumer // as of React v16.6.0
state = { data: "I'm a string stored in ExampleChildComponent.state.data" }
render(){
console.log(this.context.state.data) // I'm a string stored in App.state.data
console.log(this.state.data) // I'm a string stored in ExampleChildComponent.state.data
return (
<div>App.state.data = { this.context.state.data }</div>
<button onClick={ (e)=>{ this.context.setState({ data: this.context.state.data + ' clicked' })>
Add 'clicked' to App.state.data
</button>
<div>local component state.data = { this.state.data }</div>
<button onClick={ (e)=>{ this.setState({ data: this.state.data + ' clicked' })>
Add 'clicked' to ExampleChildComponent.state.data
</button>
)
}
}
export default ExampleChildComponent
Shows:
I'm a string stored in App.state.data
Add 'clicked' to App.state.data
I'm a string stored in ExampleChildComponent.state.data
Add 'clicked' to ExampleChildComponent.state.data
<StateConsumer>
{({ state, setState }) => (
<div>App.state.data = { state.data }</div>
)}
</StateConsumer>
import React, { Component, createContext } from 'react'
const { Provider, Consumer } = createContext()
export const StateConsumer = Consumer
export class StateProvider extends Component {
static defaultProps = {
state: {}
}
state = this.props.state
render () {
return (
<Provider value={{ state: this.state, setState: this.setState.bind(this) }}>
{ this.props.children }
</Provider>
)
}
}
- Based on ideas in this article by the author of
use-simple-state
- Uses technique explained by an article on how to use
contextType
to access context in class-based components (much like the old unofficial context API)
- Built according to this tutorial to allow publishing the ES6 React JSX code as an NPM module
- This module has evolved beyond the tutorial and now supports building the
static
keyword by including babel presetstage-0