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

113
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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