import "./styles.css";
import { useState } from "react";
const App = () => {
const [counter, setCounter] = useState({ count: 0 });
const incrementUp = () => {
// setCounter((counter) => {
// counter.count = counter.count + 1;
// return counter;
// });
setCounter((counter) => {
return {...counter, count: counter.count + 1}
})
};
return (
<div>
<div>
{counter.count}
<button onClick={incrementUp}>increment Up</button>
</div>
</div>
);
};
export default App;
Dlaczego ten counter w połączeniu z useState()
w React nie zadziała ? Chodzi mi o ten zakomentowany kod. Rozwiązanie znalazłem jednak sam nie wiem czemu do końca ta pierwsza wersja (to co jest w komentarzu) nie zadziałała.