2 回答

TA貢獻1784條經(jīng)驗 獲得超2個贊
創(chuàng)建一個類似的組件BackgroundTask并將其放入您的頁面中,其中包含一個 useEffect :
useEffect(() => {
const timer = setInterval(() => {
// call you action here
}, 5000);
return () => clearInterval(timer)
}, [])
并將其添加到 App 組件中:
export default App => (
<Provider store={store}>
<BackgroundTask/>
... rest of your app ...
</Provider>
);

TA貢獻1856條經(jīng)驗 獲得超5個贊
setTimeout()您可以通過使用函數(shù)來實現(xiàn)這一點。
您所要做的就是,在您的主要根組件內(nèi),假設(shè)您的根組件是App.js. 您可以執(zhí)行以下操作。
import React, { useEffect } from 'react';
const App = () => {
useEffect(() => {
const timer = setTimeout(() => {
/* Your API call */
}, 1000);
return () => clearTimeout(timer);
}, []);
return (
<div>
/* allYourComponents */
</div>
);
};
export default App;
添加回答
舉報