2018-09-14 10:04:29 +00:00
|
|
|
import React, { Component } from 'react'
|
|
|
|
|
|
|
|
/* First we will make a new context */
|
|
|
|
const CounterContext = React.createContext()
|
|
|
|
|
|
|
|
/* Then create a provider Component */
|
|
|
|
class CounterProvider extends Component {
|
|
|
|
state = {
|
|
|
|
count: 0
|
|
|
|
}
|
|
|
|
|
|
|
|
increase = () => {
|
|
|
|
this.setState({
|
|
|
|
count: this.state.count + 1
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-17 21:13:34 +00:00
|
|
|
increaseBy = (val) => {
|
|
|
|
this.setState({
|
|
|
|
count: this.state.count + val
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-14 10:04:29 +00:00
|
|
|
decrease = () => {
|
|
|
|
this.setState({
|
|
|
|
count: this.state.count - 1
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
return (
|
|
|
|
<CounterContext.Provider
|
|
|
|
value={{
|
|
|
|
count: this.state.count,
|
|
|
|
increase: this.increase,
|
2019-01-17 21:13:34 +00:00
|
|
|
decrease: this.decrease,
|
|
|
|
increaseBy: this.increaseBy
|
2018-09-14 10:04:29 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{this.props.children}
|
|
|
|
</CounterContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* then make a consumer which will surface it */
|
|
|
|
const CounterConsumer = CounterContext.Consumer
|
|
|
|
|
|
|
|
export default CounterProvider
|
|
|
|
export { CounterConsumer }
|