Created
April 7, 2018 15:20
-
-
Save talves/8a59acd47bf3a62b17035fa04f370a5e to your computer and use it in GitHub Desktop.
MicroState via @tannerlinsley
This file contains hidden or 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 { Component } from 'react' | |
export default class MicroState extends Component { | |
constructor (props) { | |
super(props) | |
this.state = props.state | |
} | |
render () { | |
return (this.props.render || this.props.children)({ | |
setState: (...args) => this.setState(...args), | |
...this.state, | |
}) | |
} | |
} | |
// Usage | |
// <MicroState | |
// state={{ | |
// count: 0 | |
// }} | |
// render={({ count, setState }) => ( | |
// <div> | |
// <div>Count: {count}</div> | |
// <button | |
// onClick={() => setState({count: count + 1})} | |
// > | |
// Increment Count | |
// </button> | |
// </div> | |
// )} | |
// /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment