23 lines
871 B
TypeScript
23 lines
871 B
TypeScript
import { useCallback, useRef } from 'react';
|
|
import { useFocusEffect } from '@react-navigation/native';
|
|
|
|
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;
|
|
}, [])
|
|
)
|
|
}
|