我正在制作一个 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 …Run Code Online (Sandbox Code Playgroud)