Last active
June 28, 2019 20:33
-
-
Save caub/d0d979255c373fbc65d3c45900f8e1c1 to your computer and use it in GitHub Desktop.
simple react state management
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 SuxContext = React.createContext(); | |
export class SuxProvider extends React.Component { | |
state = {}; | |
render() { | |
return ( | |
<SuxContext.Provider value={{...this.state, dispatch: data => this.setState(data)}}> | |
{this.props.children} | |
</SuxContext.Provider> | |
); | |
} | |
} | |
// usage <Sux>{({foo}) => <div>{foo}</div>}</Sux> | |
export default SuxContext.Consumer; | |
export const withSux = Component => props => ( | |
<SuxContext.Consumer> | |
{data => <Component {...props} {...data} />} | |
</SuxContext.Consumer> | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment