Advertisement
Qpel

4prktk

Nov 17th, 2019
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Die:
  2.  
  3. import React, { Component } from "react";
  4.  
  5. export default class Die extends Component {
  6.   render() {
  7.     const firstDieImage = require(`./assets/${this.state.die1}.png`);
  8.     return (
  9.       <img src = {firstDieImage}
  10.         className={`fas fa-dice-${this.props.face} ${
  11.           this.props.rolling ? "shaking" : ""
  12.         }`}
  13.  
  14.       />
  15.     );
  16.   }
  17. }
  18.  
  19.  
  20. index:
  21.  
  22.  
  23. import React, { Component } from "react";
  24. import ReactDOM from "react-dom";
  25. import Die from "./Die";
  26.  
  27. //import "./styles.css";
  28. class RollDice extends Component {
  29.  
  30.   static defaultProps = {
  31.     sides: [1, 2, 3, 4, 5, 6]
  32.   };
  33.   state = {
  34.     die1: 1,
  35.     die2: 2,
  36.     rolling: false
  37.   };
  38.  
  39.   roll = () => {
  40.    
  41.  
  42.     const newDie1 = this.props.sides[
  43.       Math.floor(Math.random() * this.props.sides.length)
  44.     ];
  45.     const newDie2 = this.props.sides[
  46.       Math.floor(Math.random() * this.props.sides.length)
  47.     ];
  48.  
  49.     this.setState({
  50.       die1: newDie1,
  51.       die2: newDie2,
  52.       rolling: true
  53.     });
  54.  
  55.     setTimeout(() => {
  56.       this.setState({ rolling: false });
  57.     }, 1000);
  58.   };
  59.  
  60.   render() {
  61.    
  62.    
  63.     return (
  64.      
  65.       <div className="roll-dice">
  66.         <div className="roll-dice-container">
  67.           <Die face={this.state.die1} rolling={this.state.rolling} />
  68.           <Die face={this.state.die2} rolling={this.state.rolling} />
  69.         </div>
  70.         <button onClick={this.roll} disabled={this.state.rolling}>
  71.           {this.state.rolling ? "Rolling..." : "Roll Dice!"}
  72.         </button>
  73.         {this.state.die1} abc {this.state.die2} cda
  74.       </div>
  75.     );
  76.   }
  77. }
  78.  
  79. const rootElement = document.getElementById("app");
  80. ReactDOM.render(<RollDice />, rootElement);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement