渲染太多,反应 useInterval
我的 useInterval 钩子有以下问题,我试图让计数器在计数达到 20 时停止,但似乎我当前实现它的方式似乎会导致渲染无限循环。到目前为止,这是我的代码:
import React, { ChangeEvent, useState } from "react";
import { useInterval } from "usehooks-ts";
export default function Component() {
// The counter
const [count, setCount] = useState<number>(0);
// Dynamic delay
const [delay, setDelay] = useState<number>(1000);
// ON/OFF
const [isPlaying, setPlaying] = useState<boolean>(true);
useInterval(
() => {
// Your custom logic here
setCount(count + 1);
},
// Delay in milliseconds or null to stop it
isPlaying ? delay : null
);
if (count === 10) {
setPlaying(false);
}
const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
setDelay(Number(event.target.value));
};
return (
<>
<h1>{count}</h1>
<button onClick={() => setPlaying(!isPlaying)}>
{isPlaying ? "pause" : "play"}
</button>
<p>
<label htmlFor="delay">Delay: </label>
<input
type="number"
name="delay"
onChange={handleChange}
value={delay}
/>
</p>
</>
);
}
我无法理解为什么这不起作用,有人可以解释并告诉我我做错了什么吗?
I have the following problem with the useInterval hook, Im trying to make the counter stop if it hits count 20, but it seems that the way im currently implementing it it seems to cause infinite loop of render. here is my code so far:
import React, { ChangeEvent, useState } from "react";
import { useInterval } from "usehooks-ts";
export default function Component() {
// The counter
const [count, setCount] = useState<number>(0);
// Dynamic delay
const [delay, setDelay] = useState<number>(1000);
// ON/OFF
const [isPlaying, setPlaying] = useState<boolean>(true);
useInterval(
() => {
// Your custom logic here
setCount(count + 1);
},
// Delay in milliseconds or null to stop it
isPlaying ? delay : null
);
if (count === 10) {
setPlaying(false);
}
const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
setDelay(Number(event.target.value));
};
return (
<>
<h1>{count}</h1>
<button onClick={() => setPlaying(!isPlaying)}>
{isPlaying ? "pause" : "play"}
</button>
<p>
<label htmlFor="delay">Delay: </label>
<input
type="number"
name="delay"
onChange={handleChange}
value={delay}
/>
</p>
</>
);
}
I can't grasp on why this wont work, could someone explain and show me what im doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
代码中的以下块是导致无限循环的原因,因为在计数为 10 且计数器暂停后的每次渲染中,每次都会执行 setPlaying(false) 并导致每次重新渲染,从而导致无限循环
相反,您可以将此块移动到使用效果,如
Please find codesandbox 供参考
The following block in the code is what's causing the infinite loop, as on every render after the count is 10 and counter is paused,
setPlaying(false)
will be executed every time and will cause re render everytime, thus cause the infinite loopInstead you can move this block to a use effect as
Please find codesandbox for reference
使用 useEffect 钩子:
每次 count 更改时,useEffect 都会触发并检查 count 是否达到 10。
您还可以阻止用户更改高于 10 的状态:
With a useEffect hook :
Every time count changes, useEffect will trigger and check if count has reached 10.
You may also prevent user from changing state above 10 :
每个更新状态或属性都会导致创建一个新的间隔。
如果您必须使用间隔,
请将此代码放入 useEffect
each update state or props cause to create a new interval.
If you have to use interval
you put this code to useEffect