admin管理员组文章数量:1391964
In a video I saw creating a progress bar and updated its value using state and wrapped it inside setInterval
for updating it every 15 millisecond and the whole being wrapped inside useEffect
, with no dependency. As state change triggers a UI update, the component re-renders and state value changes, consoling the value should print from 3000 till 0 (or in short in descending order) but it only prints 3000 (200 times) why?
const [progressBar, setProgressBar] = useState(3000);
useEffect(() => {
const progress = setInterval(() => {
console.log(progressBar);
setProgressBar((prevVal) => prevVal - 15);
}, 15);
return () => clearInterval(progress);
}, []);
If there is no dependency means the contents inside useEffect
should be executed only once know? Kindly explain.
In a video I saw creating a progress bar and updated its value using state and wrapped it inside setInterval
for updating it every 15 millisecond and the whole being wrapped inside useEffect
, with no dependency. As state change triggers a UI update, the component re-renders and state value changes, consoling the value should print from 3000 till 0 (or in short in descending order) but it only prints 3000 (200 times) why?
const [progressBar, setProgressBar] = useState(3000);
useEffect(() => {
const progress = setInterval(() => {
console.log(progressBar);
setProgressBar((prevVal) => prevVal - 15);
}, 15);
return () => clearInterval(progress);
}, []);
If there is no dependency means the contents inside useEffect
should be executed only once know? Kindly explain.
1 Answer
Reset to default 2If there is no dependency means the contents inside
useEffect
should be executed only once know?
Yes, the effect ran once and instantiated a callback function that is called on an interval with a set Javascript Closure.
The interval is why the callback is called repeatedly, and the closure is why you see the same progressBar
value each time. The reason the setProgressBar
call works is because it is working from a separate closure that is passed the current state value, e.g. the callback passed to setProgressBar
.
If you would like to see the state update you can use a separate effect with a dependency on the state value that is updating.
const [progressBar, setProgressBar] = useState(3000);
// Run once to instantiate interval callback to enqueue state updates
useEffect(() => {
const progress = setInterval(() => {
setProgressBar((prevVal) => prevVal - 15);
}, 15);
return () => clearInterval(progress);
}, []);
// Run each time `progressBar` state updates
useEffect(() => {
console.log(progressBar);
}, [progressBar]);
本文标签:
版权声明:本文标题:javascript - Why does consoling prints the same value but state gets updated when used inside useEffect - React - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744586441a2614225.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论