function App() {
const [buttonClick, setButtonClick] = useState('No')
async function handleClick(){
setButtonClick('Yes')
const sleep = (ms) ={return new Promise(resolve => setTimeout(resolve, ms))}
await sleep(2000)
console.log('this ran after state was changed')
}
return(
<>
<button onclick={handleClick}>Click me</>
{buttonClick}
</>
)
}
How come if I click the button, which sets the state of buttonClick, does it not break out of the function? It still sleeps for 2 seconds then logs my message in the console.
My thinking is that since it causes a component re-render, it would lead me to believe that the function App is returning and would break out of the handleClick function.
My thoughts on why this might be is that it might be when React is compiled it is just storing the value of the return somewhere else, and not actually returning the broader function App().