Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
React clickHandler for changing useState of a class Name logs correctly on console but does not display changes on screen when rendered

I am working on a dice roller project that changes the faces of two die on a button click. The die is both a Font Awesome icon that changes the face given a given class Name (i.e "fas fa-dice-six" renders the side with six dots).

Here is the code I have so far for the DiceRoll component:

import "./RollDice.css";
import React, { useState } from "react";
import Die from "../Die/Die";

/**
 * @returns a parent component (rendered by App)
 * that renders the dice and a button to roll
 */

const RollDice = () => {
  let sides = ["one", "two", "three", "four", "five", "six"];
  /** sets both die faces to be one at initial render */
  let [dice, setDice] = useState({
    dieOne: "one",
    dieTwo: "one",
  });

  const handleRollClick = () => {
    /** variables for each die that generates a random number for the face */
    let randomDieOne = sides[Math.floor(Math.random() * sides.length)];
    let randomDieTwo = sides[Math.floor(Math.random() * sides.length)];

    /** sets the state of each die to a random number on click */
    setDice({ dieOne: randomDieOne, dieTwo: randomDieTwo });
    console.log("randomDieOne:", randomDieOne);
    console.log("randomDieTwo:", randomDieTwo);
  };

  return (
    <div>
      <Die face={dice.dieOne} />
      <Die face={dice.dieTwo} />
      <button onClick={handleRollClick}>Roll Dice</button>
    </div>
  );
};

export default RollDice;

Here is the code I have so far for the DiceRoll component:

import React, { useState } from "react";
import RollDice from "../RollDice/RollDice";
import "./Die.css";

/**
 * @returns an individual die that takes props and
 * displays the correct face of the die based on props
 */

const Die = ({ face }) => {
  console.log("face", face);
  return (
    <div className="dice die-icon">
      <i className={`fas fa-dice-${face}`}></i>
    </div>
  );
};

export default Die;

I have a handler for clicking the "roll" button that updates the state of the number string at the end of the icon's class Name. When I log out the updated state of the die, new random numbers are logged out, however, the changes are not rendering on the screen, meaning that the class Names for the icons are not changing when the button is clicked.

Does anyone see where I may have gone wrong?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!