Created
September 7, 2018 08:53
-
-
Save unsign3d/caec3343ada1b03b87d998beb2c6b5a5 to your computer and use it in GitHub Desktop.
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 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