Skip to content

Instantly share code, notes, and snippets.

@talves
Created April 7, 2018 15:20
Show Gist options
  • Select an option

  • Save talves/8a59acd47bf3a62b17035fa04f370a5e to your computer and use it in GitHub Desktop.

Select an option

Save talves/8a59acd47bf3a62b17035fa04f370a5e to your computer and use it in GitHub Desktop.

Revisions

  1. talves created this gist Apr 7, 2018.
    32 changes: 32 additions & 0 deletions MicroState.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    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>
    // )}
    // />