Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

200
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda