Skip to content

Instantly share code, notes, and snippets.

@mzibari
Created April 17, 2020 01:50
Show Gist options
  • Save mzibari/585046f19f26e3b0355b9bee883277a2 to your computer and use it in GitHub Desktop.
Save mzibari/585046f19f26e3b0355b9bee883277a2 to your computer and use it in GitHub Desktop.
import React from 'react';
class Bomb extends React.Component {
state = {
count: 0
};
componentDidMount() {
this.interval = setInterval(() => {
this.setState({
count: this.state.count + 1
})
}, 1000)
}
componentWillMount() {
clearInterval(this.interval);
}
tickTock() {
let alert;
if (this.state.count % 2 === 0) {
alert = 'tick';
}
else if (this.state.count >= 8) {
alert = "BOOM!";
clearInterval(this.interval);
}
else {
alert = 'tock';
}
return alert;
}
render() {
return (
<div>
<p>{this.tickTock()}</p>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment