From d782e7f330cf5287193706f51f410004f123a1ce Mon Sep 17 00:00:00 2001 From: djmil Date: Mon, 31 Jul 2023 18:25:41 +0200 Subject: [PATCH] lift up state again --- src/App.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/App.js b/src/App.js index 7115bfb..0841d47 100644 --- a/src/App.js +++ b/src/App.js @@ -13,10 +13,7 @@ function Square({value, onSquareClick}) { ); } -function Board() { - const [xIsNext, setXIsNext] = useState(true); - const [squares, setSquares] = useState(Array(9).fill(null)); - +function Board({ xIsNext, squares, onPlay }) { function handleClick(i) { if (squares[i] || calculateWinner(squares)) { return; @@ -27,8 +24,7 @@ function Board() { } else { nextSquares[i] = "O"; } - setSquares(nextSquares); - setXIsNext(!xIsNext); + onPlay(nextSquares); } const winner = calculateWinner(squares); @@ -62,10 +58,19 @@ function Board() { } 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 (
- +
    {/*TODO*/}