我正在关注useState和useRef钩子的初学者教程,尝试在 react 中实现一个简单的计时器。
我正在使用interval变量来存储值setInterval()
单击开始按钮后,我可以正确地控制台记录间隔的值。但是,单击停止按钮时,interval.current控制台记录为undefined. 因此stopTimer()没有按预期运行。
为什么在 startTimer 中明确设置(并记录在那里)时,interval.current 打印未定义?我在这里想念什么?
import React, { useState, useRef } from 'react';
const pad = (time) => {
return time.toString().padStart(2, "0");
};
function App() {
const [title, setTitle] = useState("Pomodoro!");
const [timeLeft, setTimeLeft] = useState(5);
const interval = useRef(null);
const startTimer = () => {
interval.current = setInterval(() => {
setTimeLeft((timeLeft) => {
if (timeLeft >= 1) {
return timeLeft - 1;
}
return 0;
});
}, 1000);
console.log(interval.current, " :in start");
}
const stopTimer = (interval) => {
console.log("in stop: ", interval.current);
clearInterval(interval.current);
}
const resetTimer = () => { }
const minutes = pad(Math.floor(timeLeft / 60));
const seconds = pad((timeLeft - minutes * 60));
return (
<div>
<div>{title}</div>
<div>
<span>{minutes}</span>
<span>:</span>
<span>{seconds}</span>
</div>
<div>
<button onClick={startTimer}>Start</button>
<button onClick={stopTimer}>Stop</button>
<button onClick={resetTimer}>Reset</button>
</div>
</div>
);
}
export default App;
在控制台输出
6 ":in start" in stop: 未定义
谢谢