我正在测试React.useEffect
,并注意到一个我无法解释的有趣行为.
当我运行下面的代码并单击该按钮几次时,我看到以下输出缺少count=1的useEffect控制台
import React from 'react';
export function App(props) {
const [count, setCount] = React.useState(0)
console.log('ran app')
React.useEffect(() => {
console.log("ran effect", count)
}, [count < 2])
return (
<div className='App'>
<button onClick={() => setCount(count + 1)}>click</button>
<h3>{count}</h3>
</div>
);
}
ran app
ran effect
0
ran app // this is where I expect to see ran effect for count = 1 but I don't
ran app
ran effect
2
ran app
ran app
但是,如果我从依赖项中删除表达式count<2
,我也会看到count = 1
的ran useeffect
.