2 回答

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超2個(gè)贊
創(chuàng)建一個(gè)類似的組件BackgroundTask并將其放入您的頁(yè)面中,其中包含一個(gè) 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貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超5個(gè)贊
setTimeout()您可以通過使用函數(shù)來實(shí)現(xiàn)這一點(diǎ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;
添加回答
舉報(bào)