Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

How to create a Dice Rolling App using ReactJS ?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In this article, we will create a Dice rolling application that rolls two dice and displays a random number between 1 and 6. As we click the button both dices shake and generate a new number that shows on the upper face of the dice (in dotted form as a standard dice). The numbers shown on the upper face generates randomly each time we roll the dice.

There are two components Die and RollDice. The Die component is responsible to show one individual Dice. It is a stateless component. It uses the font-awesome library to show the standard dots on the upper face of the dice. RollDice component contains all the logic to generate random numbers to show on the upper face of the dice, roll each dice upon click on the roll button. There are two states involved in the RollDice component named ‘die1’ and ‘die2’. Each one has initialized with the value one i.e. each die shows one(one dot) when the application first starts. 

Now, we set the click event handler to the roll dice button and when anyone clicks on the button we change the state of both die1 and die2 to some random number using setState from one to six (we use a number as a word since font-awesome library deals with word number to show the exact number of dots). We also make sure when the dice are rolling user can’t click on the button again. For this purpose we use a state ‘rolling’ initially set to false and when the dice are rolling set rolling to true and start a timer of one second. After one second again set the rolling state to false’. Disable the button when a rolling state is set to true.

Lets have a quick look at how the final application will look like:

Dice Rolling App using ReactJS

Dice Rolling App using ReactJS 

Steps to create the Application:

  • Create the application using the following command:
npx create-react-app dice-roll
  • Navigate to the project folder
cd dice-roll
  • Install the required modules.
npm i @fortawesome/fontawesome-svg-core
npm i @fortawesome/free-solid-svg-icons
npm i @fortawesome/react-fontawesome
  • Create a new directory called components
mkdir components
  • Create the required files in the components folder that are RollDice.js, Die.js, RollDice.css and Die.css

Folder Structure:


The updated dependencies in package.json will look like:

"dependencies": {
    "@fortawesome/fontawesome-svg-core": "^6.4.0",
    "@fortawesome/free-solid-svg-icons": "^6.4.0",
    "@fortawesome/react-fontawesome": "^0.2.0",
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-scripts": "5.0.1",
    "web-vitals": "^2.1.4"

Example: App component  renders a single RollDice component only.


// App.js File
import React from 'react';
import RollDice from './components/RollDice'
import { library } from '@fortawesome/fontawesome-svg-core'
import { fas } from '@fortawesome/free-solid-svg-icons'
function App() {
  return (
      <RollDice />
export default App;

RollDice.js: It contains all the behind logic. The setting event handler updates all the states according to the user’s interaction render Die component. This file has to be created by you.

RollDice.css: Styling RollDice component contents


// RollDice.js File
import React,{ Component } from 'react'
import './RollDice.css'
import Die from './Die'
class RollDice extends Component{
  // Face numbers passes as default props
  static defaultProps = {
    sides : ['one', 'two', 'three'
             'four', 'five', 'six']
    // States
    this.state = {
      die1 : 'one',
      die2 : 'one',
      rolling: false
    this.roll = this.roll.bind(this)
    const {sides} = this.props
      // Changing state upon click
      die1 : sides[(Math.floor(Math.random() * sides.length))],
      die2 : sides[(Math.floor(Math.random() * sides.length))],
      rolling: true
    // Start timer of one sec when rolling start
    setTimeout(() => {
      // Set rolling to false again when time over
    const handleBtn = this.state.rolling ? 
                      'RollDice-rolling' : ''
    const {die1, die2, rolling} = this.state
      <div className='RollDice'>
        <div className='RollDice-container'>
          <Die face={die1} rolling={rolling}/>
          <Die face={die2} rolling={rolling}/>
        <button className={handleBtn}
          {this.state.rolling ? 'Rolling' : 'Roll Dice!'}
export default RollDice


/* RollDice.css File */
  display: flex;
  flex-flow: column nowrap;
  min-height: 100vh;
/* Shows each dice in one row */
  display: flex;
  justify-content: center;
  align-content: center;
/* Styling rolldice button */
.RollDice button{
  border: 0px;
  border-radius: 10px;
  margin-top: 3em;
  align-self: center;
/* Setting hover effect on button */
.RollDice button:hover{
  cursor: pointer;
  border: 0px;
  border-radius: 10px;
  background-color:darkslateblue !important;

Die.js: Responsible to show single-die component only with the correct dotted number face as communicated by the parent RollDice component. This file has to be created by you.

Die.css: Styling each die component and setting animation effects to it.


// Die.js File
import React, {Component} from 'react'
import './Die.css'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
class Die extends Component{
    const {face, rolling} = this.props
    // Using font awesome icon to show
    // the exactnumber of dots
    return (
                <FontAwesomeIcon icon={['fas', `fa-dice-${face}`]} className={`Die 
                ${rolling && 'Die-shaking'}`} />
            </div >
export default Die


/* Die.css File */
/* Styling each Die */
/* Applying Animation */
  animation-duration: 1s;
/* Setting Animation effect to the 
   dice using css keyframe */
@keyframes wobble {
  from {
    transform: translate3d(0, 0, 0);
  15% {
    transform: translate3d(-25%, 0, 0
               rotate3d(0, 0, 1, -5deg);
  30% {
    transform: translate3d(20%, 0, 0
               rotate3d(0, 0, 1, 3deg);
  45% {
    transform: translate3d(-15%, 0, 0)
               rotate3d(0, 0, 1, -3deg);
  60% {
    transform: translate3d(10%, 0, 0
               rotate3d(0, 0, 1, 2deg);
  75% {
    transform: translate3d(-5%, 0, 0
               rotate3d(0, 0, 1, -1deg);
  to {
    transform: translate3d(0, 0, 0);

Steps to run the Application:

  • Type the following command in the terminal:
npm start
  • Type the following URL in the browser:

Output :

Dice Rolling App using ReactJS

My Personal Notes arrow_drop_up
Last Updated : 08 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials