Created
October 18, 2017 05:30
-
-
Save ryanflorence/25e360fdc0830aa425d7ecca33099149 to your computer and use it in GitHub Desktop.
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 React from "react" | |
import { Machine } from "xstate" | |
const createMachineComponent = chart => Comp => { | |
const machine = Machine(chart) | |
return class Machine extends React.Component { | |
state = { | |
state: machine.getInitialState() | |
} | |
updateMachine = (action, payload) => { | |
this.setState({ | |
state: machine.transition(this.state.state, action).toString() | |
}) | |
} | |
render() { | |
return ( | |
<Comp {...this.props} {...this.state} update={this.updateMachine} /> | |
) | |
} | |
} | |
} | |
export default createMachineComponent |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment