Clear Interval不使用功能组件在React应用程序中工作
我想在 react> react
使用功能组件
及以下构建计时器应用程序。
该组件将显示一个初始化为 0
的数字已知为 Counter
。
该组件将在 Counter
编号下方显示开始
按钮。
单击开始
按钮,计数器将开始运行。这意味着 Counter
号码将每秒开始增加1个。
当计数器运行(增量)时, start
按钮将成为暂停
按钮。
单击暂停
按钮时, counter
将保留其值(数字),但停止运行(增量)。
组件还将显示重置
按钮。
单击重置
按钮时,计数器
将转到其初始值(在我们的情况下为 0
)并停止运行(增量)。
以下是我已实施的代码,但是 clear Interval
似乎无法正常工作,还如何实现重置按钮?
代码:
import React, { useState, useEffect } from "react";
export default function Counter() {
const [counter, setCounter] = useState(0);
const [flag, setFlag] = useState(false);
const [isClicked, setClicked] = useState(false);
var myInterval;
function incrementCounter() {
setClicked(!isClicked);
if (flag) {
myInterval = setInterval(
() => setCounter((counter) => counter + 1),
1000
);
setFlag(false);
} else {
console.log("sasdsad");
clearInterval(myInterval);
}
}
function resetCounter() {
clearInterval(myInterval);
setCounter(0);
}
useEffect(() => {
setFlag(true);
}, []);
return (
<div>
<p>{counter}</p>
<button onClick={incrementCounter}>
{isClicked ? "Pause" : "Start"}
</button>
<button onClick={resetCounter}>Reset</button>
</div>
);
}
CODESANDBOX链接: codesandbox
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我做了一个稍微不同的版本,该版本使用额外的
使用
在iSrunning
上运行(从flag
更改名称)更改:demo: https://codesandbox.io/s/dank-night-night-wwxqz3?file= /src/counter.js
作为一个额外的额外,我制作了一个使用自定义挂钩
usetimer
的版本。通过这种方式,组件代码是清洁的:I did a slightly different version that use an extra
useEffect
that runs onisRunning
(changed name fromflag
) change:Demo: https://codesandbox.io/s/dank-night-wwxqz3?file=/src/Counter.js
As a little extra i've made a version that uses a custom hook
useTimer
. In this way the component code is way cleaner:https://codesandbox.io/s/agitated-curie-nkjf62?file=/src/Counter.js
使用
useref
将间隔作为参考。然后使用resetCounter()
清洁间隔参考。Use
useRef
to make the interval as a ref. Then useresetCounter()
to clean the interval ref.我将把它留在这里给任何遇到同样问题的人。
就我而言,问题是使用节点setInterval而不是window.setInterval。
这是一个问题,因为这返回一种node.timer,它是一个对象而不是数字(setInterval ID),以使clearInterval()在需要数字的参数类型时工作。因此,要解决此问题,
或在类组件中使用componentwillmount()
I'll just leave this here for anyone having the same problem.
in my case, the issue was node setInterval was used instead of window.setInterval.
this is a problem since this returns a type of Node.Timer which is an object instead of number (setInterval ID) for the clearInterval() to work as it needs an argument type of number. so to fix this,
or in class components use componentWillMount()
在每个渲染变量
MyInterval
值之间都无法生存。这就是为什么您需要使用[useref
] [1]挂钩,以在每个渲染中保存此变量的引用。此外,您不需要标志功能,因为您拥有所有信息,其中包括
myclicked
变量,这是对您的代码进行修改。如果您有任何疑问,请不要犹豫。
Between each rendering your variable
myInterval
value doesn't survive. That's why you need to use the [useRef
][1] hook that save the reference of this variable across each rendering.Besides, you don't need an flag function, as you have all information with the
myClicked
variableHere is a modification of your code with those modifications. Don't hesitate if you have any question.
您必须在状态中存储
myinterval
。之后,当按钮单击时,flag
是false
,您可以清除间隔(状态下的myinterval)。You have to store
myInterval
in state. After that when button is clicked andflag
isfalse
, you can clear interval (myInterval in state).