I am having a hard time understanding how useEffect works. Like the code below, The count1 inside setInterval will always be 0 and useEffect never reaches the count2. Meaning, after the first render, useEffect runs and it goes inside setInterval and never leaves, so how does the count in h1 still get updated? If it never gets out of useEffect. I thought state changes will always trigger a re-render thus trigger another useEffect. Maybe I am missing something. Please help. Thank you!
import { useState, useEffect } from "react";
import ReactDOM from "react-dom";
function Timer() {
const [count, setCount] = useState(0);
useEffect(() => {
setInterval(() => {
setCount((count) => count + 1);
console.log("count1" + count);
}, 1000);
console.log("count2" + count);
}, []);
return <h1>I've rendered {count} times!</h1>;
}
export default Timer;