From 382a6592798b6376adf7ef7c7a498a79e360aa65 Mon Sep 17 00:00:00 2001 From: djmil Date: Mon, 31 Jul 2023 17:34:55 +0200 Subject: [PATCH] lisft up state --- src/App.js | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/App.js b/src/App.js index 10bd381..5cb99b1 100644 --- a/src/App.js +++ b/src/App.js @@ -1,17 +1,12 @@ import './App.css'; import { useState } from 'react'; -function Square() { - const [value, setValue] = useState(null); - - function handleClick() { - setValue('X') - } +function Square({value, onSquareClick}) { return ( @@ -19,22 +14,31 @@ function Square() { } function App() { + + const [squares, setSquares] = useState(Array(9).fill(null)); + + function handleClick(i) { + const nextSquares = squares.slice(); + nextSquares[i] = "X"; + setSquares(nextSquares); + } + return ( <>
- - - + handleClick(0)}/> + handleClick(1)}/> + handleClick(2)}/>
- - - + handleClick(3)}/> + handleClick(4)}/> + handleClick(5)}/>
- - - + handleClick(6)}/> + handleClick(7)}/> + handleClick(8)}/>
);