Please note the question is NOT about Asynchronous Nature of useState. It is Asynchronous and it's well answered here: useState set method not reflecting change immediately Please donot confuse this with that! I have used useEffect to get the button working, as shared in codepen
My question is can i add / include any asynchronous function within main component? Main Component: HelloWorld Sub function: interestShown => Can this be asynchronous? in the example i have simplified it as much as possible with console.log output.
Is this an advisable route or am i attempting something bizarre?
I have tried looking for solution to this problem, but I think all answers were directing me to classes, I am looking to get this going in React Functional Component if possible.
I have defined state like so:
const [interested, setInterested] = React.useState(false)
when I call this function:
function interestShown() {
console.log("Before Set State fired", interested)
setInterested(!interested)
console.log("After Set State Fired", interested)
}
The after state change console log needs to show the changed state, I couldn't get this going, I tried many options
My problem is here in this code pen: https://codepen.io/deepeshkr/pen/PowQWwv
The console log output:
pen.js:6 Before Set State fired false
pen.js:8 After Set State Fired false => this should be true, but i am guessing this is asynchronous change, this is my question how to fire this After State Change?
pen.js:14 Fire only it's true true
There are similar sounding questions, but i am not sure they are asking the same thing as i am asking or question is well explained to demonstrate the problem.