Skip to content

Commit d6416c4

Browse files
manuel.lopezmanuel.lopez
authored andcommitted
fix: fallback to useEffect when no navigation library is available
Projects using custom navigators that don't ship expo-router or @react-navigation/native crash on import with "No useFocusEffect implementation found". Instead of throwing, fall back to useEffect as a best-effort substitute.
1 parent c2a778c commit d6416c4

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

src/react-native/grab-screen.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useRef } from "react";
1+
import { useCallback, useEffect, useRef } from "react";
22
import { View, type ViewProps } from "react-native";
33
import { setFocusedScreenRef } from "./containers";
44

@@ -15,7 +15,17 @@ const getFocusEffectImpl = (): ((cb: () => void) => void) => {
1515
// Nothing we can do about it, it's not installed in the project.
1616
}
1717

18-
throw new Error("No useFocusEffect implementation found");
18+
// Fallback for projects without expo-router or @react-navigation/native.
19+
// Uses useEffect as a best-effort substitute — the callback runs on mount
20+
// instead of on screen focus, which is acceptable for single-screen setups
21+
// or custom navigators without a focus/blur lifecycle.
22+
return (cb: () => void) => {
23+
// eslint-disable-next-line react-hooks/rules-of-hooks
24+
useEffect(() => {
25+
const cleanup = cb();
26+
return typeof cleanup === "function" ? cleanup : undefined;
27+
}, [cb]);
28+
};
1929
};
2030

2131
const useFocusEffect = getFocusEffectImpl();

0 commit comments

Comments
 (0)