import React , {useState} from 'react';
function Test() {
const [addtolist, Setaddtolist] = useState();
const Onclick = () =>{
Setaddtolist('Button Clicked');
//I need the state in this function
console.log(addtolist)
}
// I can't console.log(addtolist) here
return (
<div>
<button onClick={() => Onclick()}>Test Button</button>
</div>
);
}
export default Test;
Why wont the addtolist state update on the first click? It will fire second time, but not first why(undefined)?