Skip to content

Instantly share code, notes, and snippets.

@unsign3d
Created September 7, 2018 08:53
Show Gist options
  • Save unsign3d/caec3343ada1b03b87d998beb2c6b5a5 to your computer and use it in GitHub Desktop.
Save unsign3d/caec3343ada1b03b87d998beb2c6b5a5 to your computer and use it in GitHub Desktop.
import React from 'react'
const AppContext = React.createContext({})
export class AppContextProvider extends React.Component {
constructor(props) {
super(props)
this.updateState = this.updateState.bind(this)
this.state = {
actions: {
updateState: this.updateState
},
data: {
...this.props.initialState
}
}
}
updateState(props) {
console.group();
console.log('diff: ', props)
this.setState((prevState) => {
console.log('previous state:', prevState.data)
const newState = {
actions: {...this.state.actions},
data: {
...this.state.data,
...props,
}
}
console.log('new state:', newState)
return newState
})
}
render() {
return(
<AppContext.Provider value={this.state}>
{this.props.children}
</AppContext.Provider>
)
}
}
export const AppContextConsumer = AppContext.Consumer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment