Skip to content

Instantly share code, notes, and snippets.

@ShMcK
Last active July 17, 2018 02:54
Show Gist options
  • Save ShMcK/36bc8f4fcc8f563398d1d59f68c958d3 to your computer and use it in GitHub Desktop.
Save ShMcK/36bc8f4fcc8f563398d1d59f68c958d3 to your computer and use it in GitHub Desktop.
VisualizingState: Walkman State Machine + Complexity
const events = {
  STOP: 'Stopped',
  PLAY: 'Playing',
  REWIND: 'Rewinding',
  FASTFORWARD: 'FastForwarding',
 }
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>
  <Button event='REWIND'>◀◀</Button>
  <Button event='FAST_FORWARD'>▶▶</Button>
  </div>
  )
 }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment