I'm a beginner in React and stuck with some problem. As below we can able to see that useEffect can be called for only once for the Counter Functional component below(as in the dependency array I haven't specify the count state).
But why the interval callbacks belongs to first render is not capable of sending the update instruction everytime the interval fires?
import React, { useState, useEffect } from "react";
import ReactDOM from "react-dom";
function Counter() {
const [count, setCount] = useState(0);
useEffect(() => {
const id = setInterval(() => {
setCount(count + 1);
}, 1000);
return () => clearInterval(id);
}, []);
return <h1>{count}</h1>;
}
const rootElement = document.getElementById("root");
ReactDOM.render(<Counter />, rootElement);