Skip to content

Instantly share code, notes, and snippets.

@caub
Last active June 28, 2019 20:33
Show Gist options
  • Save caub/d0d979255c373fbc65d3c45900f8e1c1 to your computer and use it in GitHub Desktop.
Save caub/d0d979255c373fbc65d3c45900f8e1c1 to your computer and use it in GitHub Desktop.
simple react state management
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