Last active
July 17, 2018 02:56
-
-
Save ShMcK/d589aa3001a59e958f5491891031eda1 to your computer and use it in GitHub Desktop.
VisualizingState: Walkman State Machine
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
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