So far I've come up two ways to do it.
Changing the countdown function to bind to (this) and then putting a return in the if(winner) statement that contains the html and the timer.
countDown()
{
setTimeout
(
function()
{
this.setState
({
squares : Array(9).fill(null),
xIsNext : true,
/*//old----------------------------------------------------------------
}),
3000
);
*///-------------------------------------------------------------------
});
}
.bind(this),
3000
);
}
And I changed the if winner to this.
if(winner)
{
status = 'Winner: ' + winner;
// Old code doesn't contain a return here.-----------------------------
return(
<div>
<div className="status">{status}</div>
<div className="board-row">
{this.renderSquare(0)}
{this.renderSquare(1)}
{this.renderSquare(2)}
</div>
<div className="board-row">
{this.renderSquare(3)}
{this.renderSquare(4)}
{this.renderSquare(5)}
</div>
<div style={{ marginBottom: 30 }} className="board-row">
{this.renderSquare(6)}
{this.renderSquare(7)}
{this.renderSquare(8)}
</div>
<Button id="reset" onClick={this.reset}>
Reset
</Button>
// Call counDown(this) after the button.-------------------------------
{this.countDown(this)}
</div>
)
}
else
{
status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O');
}
The other way I was able to get it to work was to add setTimeout() in if(winner)
if(winner)
{
status = "Winner: " + winner;
// Just add setTimeout() and it will reset after 3 seconds.------------
setTimeout
(
()=>
{
this.setState
({
squares : Array(9).fill(null),
xIsNext : true
});
},
3000
);
}
else
{
status = "Next player: " + (this.state.xIsNext ? "X" : "O");
}
Here is an EXAMPLE
I've been playing with getting a countdown timer to display and will add it if I get it done later.