about summary refs log tree commit diff
path: root/src/components/hooks/useRefreshOnFocus.ts
diff options
context:
space:
mode:
authorSamuel Newman <mozzius@protonmail.com>2024-05-03 17:57:20 +0100
committerGitHub <noreply@github.com>2024-05-03 17:57:20 +0100
commitce02a411633423fc6fb75cd274338f81fd380d61 (patch)
tree49ad3c7130ef43b640cbe4a51d9195c556569c08 /src/components/hooks/useRefreshOnFocus.ts
parent4a2d4253e54f1bf3a375c6c6ffdbd5a9b6bcc24a (diff)
downloadvoidsky-ce02a411633423fc6fb75cd274338f81fd380d61.tar.zst
add focus refresh + polling (#3846)
Diffstat (limited to 'src/components/hooks/useRefreshOnFocus.ts')
-rw-r--r--src/components/hooks/useRefreshOnFocus.ts17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/components/hooks/useRefreshOnFocus.ts b/src/components/hooks/useRefreshOnFocus.ts
new file mode 100644
index 000000000..6bf7ac8b1
--- /dev/null
+++ b/src/components/hooks/useRefreshOnFocus.ts
@@ -0,0 +1,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]),
+  )
+}