【发布时间】:2021-11-06 16:51:18
【问题描述】:
我正在使用 lodash 的延迟函数在特定时间段后调用我的函数 3 次,由于某种原因,我在该函数中看不到更新的 React 状态。 这可能是什么原因?
import React, { useEffect, useState } from 'react';
import Delays from './helper';
import { delay } from 'lodash';
const App = () => {
const [progress, setProgress] = useState(0);
console.log(progress); // Here I can see the updated state
const testFunction = () => {
console.log('inside function', progress); // here I can't see the updated state
};
useEffect(() => {
const seconds = 40;
const attempts = 3;
const milliseconds = 4000;
setInterval(() => {
setProgress((prevProgress) =>
prevProgress >= 100 ? 100 : prevProgress + 1
);
}, seconds * attempts * 10); // 1200
for (let i = 0; i < attempts; i++) {
delay(testFunction, milliseconds * (i + 1));
}
}, []);
return <div>Hello World</div>;
};
export default App;
对我来说奇怪的是,如果我使用基于类的方法,那么我没有任何问题,并且状态的值将出现在我的函数中。
import React, { Component } from 'react';
import Delays from './helper';
import { delay } from 'lodash';
class App extends Component {
state = {
progress: 0,
};
udpateProgress = (value) => {
this.setState({ progress: value });
};
testFunction = () => {
console.log('inside function', this.state.progress); // here I can see the Updated state
};
componentDidMount = () => {
const seconds = 40;
const milliseconds = 4000;
const attempts = 3;
setInterval(() => {
this.udpateProgress(
this.state.progress >= 100 ? 100 : this.state.progress + 1
);
}, seconds * attempts * 10); // 1200
for (let i = 0; i < attempts; i++) {
delay(this.testFunction.bind(this), milliseconds * (i + 1));
}
};
render() {
console.log('inside render', this.state.progress);
return <div>Hello World</div>;
}
}
export default App;
【问题讨论】:
标签: reactjs react-hooks lodash use-effect use-state