DEV Community

Cover image for How to create a Dice Rolling App using ReactJS
Moha Saket
Moha Saket

Posted on

How to create a Dice Rolling App using ReactJS

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 generates random numbers to show on the upper face of the dice, roll each dice upon click on the roll button. There are two states involve 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 is 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 rolling state to false’. Disable the button when a rolling state is set to true.
This is a rough overview of the application. go to this link
Image description

Top comments (0)