let guest = 0;
function Cup({name}) {
guest = guest + 1;
console.log(name,guest);
return <h2>Tea cup for guest #{guest}</h2>;
}
return (
<>
<Cup name={"one"}/>
<Cup name={"two"}/>
<Cup name={"three"}/>
</>
)
Output : Output | Console Output
The guest variable is not a state and changing it will not cause a re-render right. ?
I know i should use a state but i want to know how this code executes and why
2
Answers
This was given by chatGPT but i do not know it is the real reason
You are using same component in render but its three different component to render,so the guest variable will have its 3 instances.
1 component 1 variable.
So,if i am not wrong 3 component 3 variable are there.
its working as it is supposed to.