I'm new to coding and have done some online courses with JS and am currently going through the ReactJS Tic Tac Toe tutorial (here). I understand the general logic, but am confused by what the constructor in the Board class is doing:
class Board extends React.Component {
constructor(props) {
super(props);
this.state = {
squares: Array(9).fill(null),
};
}
renderSquare(i) {
return <Square value={i} />;
}
render() {
const status = 'Next player: X';
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 className="board-row">
{this.renderSquare(6)}
{this.renderSquare(7)}
{this.renderSquare(8)}
</div>
</div>
);
}
}
From what I understand, it's initializing the board and the board state, but when I fiddle with the code and change the number of squares as defined in the null array it doesn't change the functionality of the board/game. If I delete the array out, however, the code crashes. What is the purpose of the constructor and why does changing the array length to not equal the number of squares on the board not affect the game, but not including the array does?