about summary refs log tree commit diff
path: root/src/components/hooks/useRefreshOnFocus.ts
blob: 6bf7ac8b1f9ea3ae141552faf8a016be1e9a5a5e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import {useCallback, useRef} from 'react'
import {useFocusEffect} from '@react-navigation/native'

export function useRefreshOnFocus<T>(refetch: () => Promise<T>) {
  const firstTimeRef = useRef(true)

  useFocusEffect(
    useCallback(() => {
      if (firstTimeRef.current) {
        firstTimeRef.current = false
        return
      }

      refetch()
    }, [refetch]),
  )
}