Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Created October 18, 2017 05:30
Show Gist options
  • Save ryanflorence/25e360fdc0830aa425d7ecca33099149 to your computer and use it in GitHub Desktop.
Save ryanflorence/25e360fdc0830aa425d7ecca33099149 to your computer and use it in GitHub Desktop.
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