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

111
Views
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 answers
Answer question

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 Report
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!