function useStateTrap() {
const [count, setCount] = useState(0);
useEffect(() => {
const timer = setInterval(() => {
setCount(count + 1);
console.log("setInterval", count);
}, 1000);
return () => clearInterval(timer);
}, []);
return <div>count:{count}</div>;
}
页面显示count
为1,说明setCount(count + 1)
生效了,因为count
初始值是0,那么请问打印结果为什么依然是 setInterval 0
?