lisft up state

This commit is contained in:
djmil 2023-07-31 17:34:55 +02:00
parent d59be919e5
commit 382a659279

View File

@ -1,17 +1,12 @@
import './App.css'; import './App.css';
import { useState } from 'react'; import { useState } from 'react';
function Square() { function Square({value, onSquareClick}) {
const [value, setValue] = useState(null);
function handleClick() {
setValue('X')
}
return ( return (
<button <button
className="square" className="square"
onClick={handleClick} onClick={onSquareClick}
> >
{value} {value}
</button> </button>
@ -19,22 +14,31 @@ function Square() {
} }
function App() { function App() {
const [squares, setSquares] = useState(Array(9).fill(null));
function handleClick(i) {
const nextSquares = squares.slice();
nextSquares[i] = "X";
setSquares(nextSquares);
}
return ( return (
<> <>
<div className="board-row"> <div className="board-row">
<Square /> <Square value={squares[0]} onSquareClick={() => handleClick(0)}/>
<Square /> <Square value={squares[1]} onSquareClick={() => handleClick(1)}/>
<Square /> <Square value={squares[2]} onSquareClick={() => handleClick(2)}/>
</div> </div>
<div className="board-row"> <div className="board-row">
<Square /> <Square value={squares[3]} onSquareClick={() => handleClick(3)}/>
<Square /> <Square value={squares[4]} onSquareClick={() => handleClick(4)}/>
<Square /> <Square value={squares[5]} onSquareClick={() => handleClick(5)}/>
</div> </div>
<div className="board-row"> <div className="board-row">
<Square /> <Square value={squares[6]} onSquareClick={() => handleClick(6)}/>
<Square /> <Square value={squares[7]} onSquareClick={() => handleClick(7)}/>
<Square /> <Square value={squares[8]} onSquareClick={() => handleClick(8)}/>
</div> </div>
</> </>
); );