follow the istructions to create a closure of the state variable counter
set the counter to preferred value start a timeout and wait for {timeoutinseconds} to increment the counter (current value is {counter}) increment by 10 the counter before the timeout
{ renderlogs() }
);}export default app;登录后复制
解决方案
解决方案基于useref钩子的使用,它可以让你引用渲染不需要的值。
所以我们在app组件中添加:
const currentcounter = useref(counter)
登录后复制
然后我们修改settimeout的回调,如下所示:
settimeout(() => { setlogs((l) => [...l, `you closed counter with value: ${currentcounter.current} and now i'll increment by one. check the state`]) settimeoutinprogress(false) setstarttimeout(false) setcounter(currentcounter.current + 1) setlogs((l) => [...l, `did you create a closure of counter?`]) }, timeoutinseconds * 1000);