With the code below - handleChange methos is used in form fields:
i = 0
i
should get updated with i+1
console.log(i)
= should be 1, but get 0Thanks
const [i, setI] = useState(0)
const handleChange = input => event => {
setI(i + 1)
console.log(i)
}
<ChildFormComponent handleChange={handleChange}/>
The setI
function is async. I is not updated synchronously but asynchronously. If you want to log the new value of i
use the useEffect
hook and add i
as its dependency.
React.useEffect(() => {
console.log(i)
}, [i])