Skip to content

Instantly share code, notes, and snippets.

@ShMcK
Last active July 17, 2018 02:56
Show Gist options
  • Save ShMcK/d589aa3001a59e958f5491891031eda1 to your computer and use it in GitHub Desktop.
Save ShMcK/d589aa3001a59e958f5491891031eda1 to your computer and use it in GitHub Desktop.
VisualizingState: Walkman State Machine
const events = {
  STOP: 'Stopped',
  PLAY: 'Playing',
 }
class Walkman extends React.Component {
 state = {
  current: events.STOP,
 }
 transition = (event) => {
  this.setState({ current: events[event] })
 }
 render() {
  const Button = ({ children, event, active }) => {
  return (
  <button onClick={() => this.transition(event)}>
  {children}
  </button>
  )
  }
  return (
 <div>
<div>{this.state.current}</div>
 <Button event='STOP'>◼</Button>
  <Button event='PLAY'>►</Button>
 </div>
  )
 }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment