Skip to content

Instantly share code, notes, and snippets.

@talves
Created April 7, 2018 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save talves/8a59acd47bf3a62b17035fa04f370a5e to your computer and use it in GitHub Desktop.
Save talves/8a59acd47bf3a62b17035fa04f370a5e to your computer and use it in GitHub Desktop.
MicroState via @tannerlinsley
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