I'm new to react.js and I started trying to create a simple Tic-Tac Toe app, but I'm having an issue with my logic.
When I console.log the current turn I have the correct "X" or "O" character tile. However, when I console.log it inside the render() function I get the opposite character tile (i.e. if the winner is "X" then the winner renders on the template as "O").
I tried swapping the value of current value inside the render() function, but it's giving me an error. Any ideas on what I'm doing wrong here?
Link to my github repo is here, or the check out the code is below. https://github.com/jchan922/tic-react-toe/blob/master/src/App.js
Thanks!
EDIT:
CodePen
Question clarification
-- even though my logic is console logging everywhere else correctly, if the winner is "X" then the winner renders on the view as "O" and vice versa. I can't get my template to render the correct "X" or "O" player.
class App extends Component {
// Component Initialization
constructor(props){
super(props)
this.state = {
player_one: "X",
player_two: "O",
currentTurn: "X",
board: [
"", "", "", "", "", "", "", "", ""
],
winner: null,
moveCount: 0
}
}
handleClick(index){
if(this.state.board[index] === "" && !this.state.winner) {
var currentBoard = this.state.board,
count = this.state.moveCount;
currentBoard[index] = this.state.currentTurn;
count += 1;
this.setState({
board: this.state.board,
winner: this.checkForWinner(),
currentTurn: this.state.currentTurn === this.state.player_one ? this.state.player_two : this.state.player_one,
moveCount: count
});
}
}
handleReset(){
console.log("RESET")
this.setState({
player_one: "X",
player_two: "O",
currentTurn: "X",
board: [
"", "", "", "", "", "", "", "", ""
],
winner: null,
moveCount: 0
})
}
checkForWinner(moveCount) {
var currentTurn = this.state.currentTurn;
var symbols = this.state.board;
var winningCombos = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]];
return winningCombos.find(function(combo) {
if(symbols[combo[0]] !== "" && symbols[combo[1]] !== "" && symbols[combo[2]] !== "" && symbols[combo[0]] === symbols[combo[1]] && symbols[combo[1]] === symbols[combo[2]]) {
console.log("Found a winner", currentTurn, [combo[0],combo[1],combo[2]]);
return currentTurn;
}
else {
return null;
}
})
}
// Component Render
render() {
console.log(this.state.currentTurn);
return (
<div className="app-container">
{this.state.winner ? <h2>{`The winner is ${this.state.currentTurn}`}</h2> : null}
<div className="board">
{this.state.board.map((cell, index) => {
return <div onClick={() => this.handleClick(index)} key={index} className="square"><p className="playerSymbol">{cell}</p></div>
})}
</div>
<div className="reset">
<button onClick={() => this.handleReset()} className="resetButton">RESET</button>
</div>
</div>
)
}
}
export default App;
