Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

193
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda