Ensure mounted state in useSetTimeout() remains consistent (#22025)

This commit is contained in:
Jon Koops 2023-07-28 11:41:40 +02:00 committed by GitHub
parent 08dfdffbfb
commit fa592f882a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,13 +4,14 @@ export default function useSetTimeout() {
const didUnmountRef = useRef(false);
const { current: scheduledTimers } = useRef(new Set<number>());
useEffect(
() => () => {
useEffect(() => {
didUnmountRef.current = false;
return () => {
didUnmountRef.current = true;
clearAll();
},
[],
);
};
}, []);
function clearAll() {
scheduledTimers.forEach((timer) => clearTimeout(timer));