英文:
I am trying to implement a counter using react but cant seem to be able to clear the intervals properly
问题
我相对于React还很新手,刚刚开始一个基本项目,但我在尝试创建一个计数器应用时遇到了困难,自动增加和自动减少功能同时执行,导致计数器无法正常工作。任何帮助将不胜感激。提前感谢。
intervalId 是在组件全局范围内定义的变量。按钮点击处理程序如下所示:
const stopInterval = () => {
clearInterval(intervalId);
intervalId = null;
};
const handleAutoDecrement = () => {
stopInterval();
if (!intervalId) {
intervalId = setInterval(() => {
setCounter((prev) => prev - 1);
}, 1000);
}
};
const handleAutoIncrement = () => {
stopInterval();
if (!intervalId) {
intervalId = setInterval(() => {
setCounter((prev) => prev + 1);
}, 1000);
}
};
我尝试在此之前在返回回调中清除了间隔,但结果相同,所以我完全不知道现在该怎么办。
英文:
I am fairly new to react and started out with a basic project but I'm struggling with a counter application that I am trying to make wherein the auto increment and the auto decrement function perform simultaneously so the count is not functioning properly. Any help would be appreciated. Thanks in advance.
intervalId is a variable that has been defined globally in the component. The button click handlers are as mentioned below.
const stopInterval = () => {
clearInterval(intervalId);
intervalId = null;
};
const handleAutoDecrement = () => {
stopInterval();
if (!intervalId) {
intervalId = setInterval(() => {
setCounter((prev) => prev - 1);
}, 1000);
}
};
const handleAutoIncrement = () => {
stopInterval();
if (!intervalId) {
intervalId = setInterval(() => {
setCounter((prev) => prev + 1);
}, 1000);
}
};
I tried clearing the intervals in a return call back prior to this but got the same result so I am completely clueless so as to do what now.
答案1
得分: 0
你正在将intervalId
存储在本地变量中,每次重新渲染后其值都变为未定义。你需要将该值存储在状态中,以便在计数器值更改并重新渲染时,它能够保持intervalId
的值。
import React, { useState } from 'react';
import './style.css';
function CounterComponent() {
const [counter, setCounter] = useState(0);
const [intervalId, setIntervalId] = useState();
const stopInterval = () => {
clearInterval(intervalId);
setIntervalId(null);
};
const handleDecrement = () => {
setCounter((prev) => prev - 1);
};
const handleAutoDecrement = () => {
stopInterval();
if (!intervalId) {
const id = setInterval(() => {
setCounter((prev) => prev - 1);
}, 1000);
setIntervalId(id);
}
};
const handleAutoIncrement = () => {
stopInterval();
if (!intervalId) {
const id = setInterval(() => {
setCounter((prev) => prev + 1);
}, 1000);
setIntervalId(id);
}
};
const handleIncrement = () => {
setCounter((prev) => prev + 1);
};
return (
<>
<div className="counterClass">{counter}</div>
<br />
<br />
<button onClick={handleDecrement} className="decrementButton">
减少
</button>
<button onClick={handleAutoDecrement} className="autoDecrementButton">
自动减少
</button>
<button onClick={handleAutoIncrement} className="autoIncrementButton">
自动增加
</button>
<button onClick={handleIncrement} className="incrementButton">
增加
</button>
<button onClick={stopInterval} className="incrementButton">
停止
</button>
</>
);
}
export default CounterComponent;
英文:
You are storing intervalId
in local variable and after every re-render its value gets undefined. You will have to store the value in a state so that when counter value changes and it renders again it must persist the intervalId
value.
import React, { useState } from 'react';
import './style.css';
function CounterComponent() {
const [counter, setCounter] = useState(0);
const [intervalId, setintervalId] = useState();
const stopInterval = () => {
clearInterval(intervalId);
setintervalId();
};
const handleDecrement = () => {
setCounter((prev) => prev - 1);
};
const handleAutoDecrement = () => {
stopInterval();
// if (!intervalId) {
setintervalId(setInterval(() => {
setCounter((prev) => prev - 1);
}, 1000));
console.log('interca', intervalId);
// }
};
const handleAutoIncrement = () => {
stopInterval();
// if (!intervalId) {
setintervalId(setInterval(() => {
setCounter((prev) => prev + 1);
}, 1000));
// }
};
const handleIncrement = () => {
setCounter((prev) => prev + 1);
};
return (
<>
<div className="counterClass">{counter}</div>
<br />
<br />
<button onClick={handleDecrement} className="decrementButton">
Decrement
</button>
<button onClick={handleAutoDecrement} className="autoDecrementButton">
AutoDecrement
</button>
<button onClick={handleAutoIncrement} className="autoIncrementButton">
AutoIncrement
</button>
<button onClick={handleIncrement} className="incrementButton">
Increment
</button>
<button onClick={stopInterval} className="incrementButton">
Stop
</button>
</>
);
}
export default CounterComponent;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论