gpt4 book ai didi

reactjs - React hook useState 的 setter inside 函数不起作用

转载 作者:行者123 更新时间:2023-12-05 05:08:28 25 4
gpt4 key购买 nike

我正在尝试对我正在从事的项目中的倒计时组件进行重构。

当我完成逻辑迁移时,计数器的值不起作用。我决定在 codesandbox 中从零开始,所以我尝试了最简单的实现并得出了这个:

import React, { useState, useEffect } from "react";
import ReactDOM from "react-dom";

import "./styles.css";

function App() {
const [counter, setCounter] = useState(60);

useEffect(() => {
const interval = setInterval(() => setCounter(counter - 1), 1000);

return () => clearInterval(interval);
}, []);

return (
<div className="App">
<h1>Hello CodeSandbox {counter}</h1>
<h2>Start editing to see some magic happen!</h2>
</div>
);
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App timerSeconds={360} />, rootElement);

这里发生的是 counter 的值在第一次运行间隔后保持在 59。

代码框:https://codesandbox.io/embed/flamboyant-moon-ogyqr

问题的第二次迭代

感谢罗斯的回复,但真正的问题发生在我将倒计时链接到处理程序时:

import React, { useState, useEffect } from "react";
import ReactDOM from "react-dom";

import "./styles.css";

function App() {
const [counter, setCounter] = useState(60);
const [countdownInterval, setCountdownInterval] = useState(null);


const startCountdown = () => {
setCountdownInterval(setInterval(() => setCounter(counter - 1), 1000));
};

useEffect(() => {
return () => clearInterval(countdownInterval);
});

return (
<div className="App" onClick={startCountdown}>
<h1>Hello CodeSandbox {counter}</h1>
<h2>Start editing to see some magic happen!</h2>
</div>
);
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App timerSeconds={360} />, rootElement);

最佳答案

useEffect 函数的第二个参数(数组)中添加 counter 变量。当您传入一个空数组时,它只会在初始渲染时更新一次 state。当您发出 HTTP 请求或类似的东西时,通常会使用空数组。 (为第二次迭代编辑)

import React, { useEffect, useState } from "react";
import ReactDOM from "react-dom";

import "./styles.css";

function App() {
const [counter, setCounter] = useState(5);
const [counterId, setCounterId] = useState(null);

useEffect(() => {
return () => clearInterval(counterId);
}, []);

const handleClick = () => {

/*
* I'd take startCountdown and make
* it's own component/hook out of it,
* so it can be easily reused and expanded.
*/
const startCountdown = setInterval(() => {
return setCounter((tick) => {
if (tick === 0) {
clearInterval(counterId);
setCounter(0);
return setCounterId(null);
};

return tick - 1;
});
}, 1000)

setCounterId(startCountdown);
};

return (
<div className="App" onClick={handleClick}>
<h1>Hello CodeSandbox {counter}</h1>
<h2>Start editing to see some magic happen!</h2>
</div>
);
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App timerSeconds={360} />, rootElement);

有关此实现的更多信息,请在 https://reactjs.org/docs/hooks-effect.html#tip-optimizing-performance-by-skipping-effects 阅读有关 React Hooks 和跳过效果的信息.

关于reactjs - React hook useState 的 setter inside 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58276594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com