我的代码:
useEffect(() => {
later.setInterval(
async () => {
console.log('refreshing...', dayjs().format('HH:mm:ss')); // eslint-disable-line
const refreshedCredentials = await refreshSession();
refreshUserSession(refreshedCredentials);
},
later.parse.text('every 25 min'),
);
}, []); // eslint-disable-line
.setInterval
应该:
为文本调度的每个连续出现执行(函数)
换句话说,只要时间匹配,就运行该函数。但是,我的函数似乎重新运行多次:
它会导致不必要的 API 请求,我担心它稍后会滞后于我的应用程序。有什么建议么?
更新:
有问题的文件包含几个 react-router 路由(MainLayout):
<MainLayout>
<Route
exact
path="/dashboard"
component={Dashboard}
/>
<Route
exact
path="/restaurant"
component={Restaurant}
/>
<Route
exact
path="/reservations"
component={Reservations}
/>
<Route
exact
path="/dishes"
component={Dishes}
/>
<Route
exact
path="/dishes/:id"
component={Dish}
/>
</MainLayout>