Last active
March 5, 2019 00:08
-
-
Save PabloRegen/97cc4544e69fae6aa3f6f087038023c5 to your computer and use it in GitHub Desktop.
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
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