lift up state again

This commit is contained in:
djmil 2023-07-31 18:25:41 +02:00
parent 455f2b1c64
commit d782e7f330

View File

@ -13,10 +13,7 @@ function Square({value, onSquareClick}) {
); );
} }
function Board() { function Board({ xIsNext, squares, onPlay }) {
const [xIsNext, setXIsNext] = useState(true);
const [squares, setSquares] = useState(Array(9).fill(null));
function handleClick(i) { function handleClick(i) {
if (squares[i] || calculateWinner(squares)) { if (squares[i] || calculateWinner(squares)) {
return; return;
@ -27,8 +24,7 @@ function Board() {
} else { } else {
nextSquares[i] = "O"; nextSquares[i] = "O";
} }
setSquares(nextSquares); onPlay(nextSquares);
setXIsNext(!xIsNext);
} }
const winner = calculateWinner(squares); const winner = calculateWinner(squares);
@ -62,10 +58,19 @@ function Board() {
} }
function App() { function App() {
const [xIsNext, setXIsNext] = useState(true);
const [history, setHistory] = useState([Array(9).fill(null)]);
const currentSquares = history[history.length - 1];
function handlePlay(nextSquares) {
setHistory([...history, nextSquares]);
setXIsNext(!xIsNext);
}
return ( return (
<div className="game"> <div className="game">
<div className="game-board"> <div className="game-board">
<Board /> <Board xIsNext={xIsNext} squares={currentSquares} onPlay={handlePlay} />
</div> </div>
<div className="game-info"> <div className="game-info">
<ol>{/*TODO*/}</ol> <ol>{/*TODO*/}</ol>