Advertisement
Guest User

clickGame

a guest
Jun 28th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3.  
  4. class SpeedClickGame extends React.Component{
  5. constructor(props){
  6. super(props),
  7. this.state = {
  8. time: this.props.time,
  9. points: 0,
  10. disabled: false,
  11. }
  12. }
  13.  
  14. handleButtonClick = () => {
  15. if(this.state.disabled == false) {
  16. this.setState({
  17. time: this.state.time - 50,
  18. points: this.state.points + 1,
  19. })
  20. }
  21. }
  22.  
  23. componentDidMount() {
  24. this.setTimeout = setTimeout( () => {
  25. this.setState({
  26. disabled: true,
  27. })
  28. }, this.state.time)
  29. }
  30.  
  31. componentWillUpdate() {
  32. clearInterval(this.setTimeout)
  33. this.setTimeout = setTimeout( () => {
  34. this.setState({
  35. disabled: true,
  36. })
  37. }, this.state.time)
  38. }
  39.  
  40. componentWillUnmount() {
  41. clearInterval(this.intervalId)
  42. }
  43.  
  44. render() {
  45. return <div>
  46. <button onClick={this.handleButtonClick}>Click me!</button>
  47. <h1>{this.state.time}ms</h1>
  48. <h2>{this.state.points}</h2>
  49. </div>
  50. }
  51. }
  52.  
  53.  
  54. document.addEventListener('DOMContentLoaded', function(){
  55. ReactDOM.render(
  56. <SpeedClickGame time={1000} />,
  57. document.getElementById('app')
  58. );
  59. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement