Skip to content

Instantly share code, notes, and snippets.

@PabloRegen
Last active March 5, 2019 00:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PabloRegen/97cc4544e69fae6aa3f6f087038023c5 to your computer and use it in GitHub Desktop.
Save PabloRegen/97cc4544e69fae6aa3f6f087038023c5 to your computer and use it in GitHub Desktop.
class App extends Component {
state = {...};
runStopButton = () => {...}
handleClearBoard = () => {...}
handleNewBoard = () => {...}
handleToggleCellStatus = () => {...}
handleStep = () => {...}
handleSpeedChange = newSpeed => {
this.setState({ speed: newSpeed });
}
handleRun = () => {
this.setState({ isGameRunning: true });
}
handleStop = () => {
this.setState({ isGameRunning: false });
}
componentDidUpdate(prevProps, prevState) {
const { isGameRunning, speed } = this.state;
const speedChanged = prevState.speed !== speed;
const gameStarted = !prevState.isGameRunning && isGameRunning;
const gameStopped = prevState.isGameRunning && !isGameRunning;
if ((isGameRunning && speedChanged) || gameStopped) {
clearInterval(this.timerID);
}
if ((isGameRunning && speedChanged) || gameStarted) {
this.timerID = setInterval(() => {
this.handleStep();
}, speed);
}
}
// Render method ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment