freshyo/packages/ui/hooks/useMarkDataFetchers.ts
2026-11-18 16:52:41 +05:30

23 lines
858 B
TypeScript

import { useCallback, useRef } from 'react';
import { useFocusEffect } from 'expo-router';
export const useMarkDataFetchers = (callback: () => void) => {
// The callback is held in a ref: call sites pass inline arrows, so putting it in the
// dependency array below would re-run the focus effect on every render and refetch in a
// loop. The ref keeps the latest closure without re-subscribing.
const callbackRef = useRef(callback);
callbackRef.current = callback;
const focusCountRef = useRef(0);
useFocusEffect(
useCallback(() => {
// Skip the initial focus only (the screen fetched on mount); refresh from the
// second focus onward. This used to be `> 1`, which skipped the first return too.
if (focusCountRef.current > 0) {
callbackRef.current();
}
focusCountRef.current += 1;
}, [])
)
}