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

112
Vistas
conditional button visibility with react hooks

I'm making a game in react and I have a reset button but I want it to only appear when the user loses the game, I tried using the useState but i'm new to it and couldn't figure out how to do it.

the game over function :

function handleCellClick(x, y) {
let updatedData = [...boardData];
// check if revealed. return if true.
if (updatedData[x][y].isRevealed || updatedData[x][y].isFlagged)
  return null;

// check if mine. game over if true
if (updatedData[x][y].isMine) {
  setGameStatus("You Lost!");
  revealBoard();

}

the reset function :

function refreshGame() {

setBoardData(initBoardData(props.height, props.width, props.mines));
setGameStatus("Game in progress");
setMineCount(10);

}

button :

    <button onClick={refreshGame} className="btn-refresh btn-outline-danger">
      Play Again?
    </button>
  </div>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

you can use conditional rendering of button, with logical AND (&&) it would be easy,
as you setting text setGameStatus("You Lost!"); we can extract out same logic to render button or not it would be decided

{
  gameStatus === "You Lost!" && 
  <button onClick={refreshGame} className="btn-refresh btn-outline-danger">
    Play Again?
  </button>
}
about 4 years ago · Juan Pablo Isaza Denunciar
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