While trying to understand React hooks, i wrote this small code:
import React, {useState} from "react";
import "./styles.css";
export default function App(props) {
var [x, setX] = useState(0);
function handleClick(e) {
setX(x++);
console.log(x);
}
return(<div className="App">
<h3 className="h"> Welcome user, please press this button:</h3>
{x}
<br/>
<button className='button' onClick={handleClick}> Click </button>
</div>);
}
I was expecting to get output 1 2 3 4 5 6...
in console whenever i press the button, but i got this weird output instead 1 2 2 3 3 4 4 5 5 6 6
.
Can someone explain why we get such output and how to fix it ?