I was interviewed with the following questions, I got the explained answer by my interviewer. But after the interview, I tried to recreate the problem. I got a different results
Here's the code and the answer is to explain what will be printed in console
function App() {
const [foo, setFoo] = useState('a');
useEffect(() => {
setFoo('b');
}, []);
useEffect(() => {
console.log("effect ", foo);
}, [foo]);
console.log("render ", foo);
return (
<div></div>
);
}
The following is my understanding ( and the interviewer's explanation)
useEffect and stored them into the stack firstSo the output supposed to be
'render a'
'effect a'
'render b'
'effect b'
but when I executed the code, what I got is
'render a'
'effect a'
'effect a'
'render b'
'effect b'
Why is there extra 'effect a'?
And is there any reading material regarding to the lifecycle of functional components?