forked from callstackincubator/react-native-grab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfocus-effect.ts
More file actions
41 lines (32 loc) · 1.08 KB
/
Copy pathfocus-effect.ts
File metadata and controls
41 lines (32 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { useEffect } from "react";
const getDefaultFocusEffectFactory = (): ((cb: () => void) => void) => {
try {
return require("expo-router").useFocusEffect;
} catch {
// Nothing we can do about it, it's not installed in the project.
}
try {
return require("@react-navigation/native").useFocusEffect;
} catch {
// Nothing we can do about it, it's not installed in the project.
}
console.warn(
"[react-native-grab] No supported router found — falling back to useEffect. This may cause issues. Provide a custom focus effect using the setFocusEffect function.",
);
const useFallbackFocusEffect = (cb: () => void) => {
useEffect(() => {
return cb();
}, [cb]);
};
return useFallbackFocusEffect;
};
let cachedFocusEffect: ((cb: () => void) => void) | null = null;
export const getFocusEffect = (): ((cb: () => void) => void) => {
if (!cachedFocusEffect) {
cachedFocusEffect = getDefaultFocusEffectFactory();
}
return cachedFocusEffect;
};
export const setFocusEffect = (impl: (cb: () => void) => void) => {
cachedFocusEffect = impl;
};