现在我每 10 秒调用一次间隔函数。我的问题是在 useEffect 我有一个具有通道 id 的依赖数组。那么当组件卸载时会调用这个 clearInterval 函数吗?
const pollCurrentConversationId = channelID => {
pollBackendStart({ metadata: { channelID } });
};
const pageFocused = () => {
if (document.hasFocus()) {
pollCurrentConversationId(channelID);
}
pollingTimerId.current = setInterval(() => {
if (document.hasFocus()) {
pollCurrentConversationId(channelID);
}
}, 10000);
};
const pageNotFocused = useCallback(() => {
if (channelID) {
pollCurrentConversationId(channelID);
}
clearInterval(pollingTimerId.current);
}, [channelID, pollBackendStart]);
useEffect(() => {
if (channelID) {
pageFocused();
}
return () => {
clearInterval(pollingTimerId.current);
};
}, [currentConversation.id]);
答案 0 :(得分:0)
在卸载组件时运行清理,无论依赖项数组如何。它也会在每次组件重新渲染时运行。