Kon*_*sel 2 setinterval reactjs use-effect
我正在制作一个 React 仪表板,每分钟都会调用 API 进行更新。根据SO中的许多答案,我现在有这样的作品:
const Dashboard = (props) => {
const [stockData, setStockData] = useState([]);
useEffect(() => {
//running the api call on first render/refresh
getAPIData();
//running the api call every one minute
const interval = setInterval(() => {
getAPIData()
}, 60000);
return () => clearInterval(interval);
}, []);
//the api data call
const getAPIData = async () => {
try {
const stdata = await DataService.getStockData();
setStockData(stdata);
}
catch (err) {
console.log(err);
}
};
Run Code Online (Sandbox Code Playgroud)
但是我不断收到浏览器警告
React Hook useEffect has a missing dependency: 'getAPIData'. Either include it or remove the dependency array
Run Code Online (Sandbox Code Playgroud)
这是否值得关注(例如导致内存泄漏)?
我尝试修复它:
我找到了一些关于这个问题的参考文献,比如 这里 和 这里 ,但我无法理解它,因为我一个月前才开始使用 React。
感谢对此的任何帮助!
| 归档时间: |
|
| 查看次数: |
8538 次 |
| 最近记录: |