TicTacToe/src/App.js

48 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-07-31 13:16:42 +02:00
import './App.css';
2023-07-31 14:28:09 +02:00
import { useState } from 'react';
2023-07-31 17:34:55 +02:00
function Square({value, onSquareClick}) {
2023-07-31 14:05:46 +02:00
return (
<button
className="square"
2023-07-31 17:34:55 +02:00
onClick={onSquareClick}
2023-07-31 14:05:46 +02:00
>
{value}
</button>
);
}
2023-07-31 13:16:42 +02:00
function App() {
2023-07-31 17:34:55 +02:00
const [squares, setSquares] = useState(Array(9).fill(null));
function handleClick(i) {
const nextSquares = squares.slice();
nextSquares[i] = "X";
setSquares(nextSquares);
}
2023-07-31 13:16:42 +02:00
return (
2023-07-31 13:35:46 +02:00
<>
2023-07-31 13:44:31 +02:00
<div className="board-row">
2023-07-31 17:34:55 +02:00
<Square value={squares[0]} onSquareClick={() => handleClick(0)}/>
<Square value={squares[1]} onSquareClick={() => handleClick(1)}/>
<Square value={squares[2]} onSquareClick={() => handleClick(2)}/>
2023-07-31 13:44:31 +02:00
</div>
<div className="board-row">
2023-07-31 17:34:55 +02:00
<Square value={squares[3]} onSquareClick={() => handleClick(3)}/>
<Square value={squares[4]} onSquareClick={() => handleClick(4)}/>
<Square value={squares[5]} onSquareClick={() => handleClick(5)}/>
2023-07-31 13:44:31 +02:00
</div>
<div className="board-row">
2023-07-31 17:34:55 +02:00
<Square value={squares[6]} onSquareClick={() => handleClick(6)}/>
<Square value={squares[7]} onSquareClick={() => handleClick(7)}/>
<Square value={squares[8]} onSquareClick={() => handleClick(8)}/>
2023-07-31 13:44:31 +02:00
</div>
2023-07-31 13:35:46 +02:00
</>
2023-07-31 13:16:42 +02:00
);
}
export default App;