one
Version:
One is a new React Framework that makes Vite serve both native and web.
52 lines (51 loc) • 2.07 kB
JavaScript
import { useEffect } from "react";
import { useOptionalNavigation } from "./link/useLoadedNavigation.mjs";
function useFocusEffect(effect, deps) {
const navigation = useOptionalNavigation();
useEffect(() => {
if (!navigation) {
return;
}
let isFocused = false;
let cleanup;
const callback = () => {
const destroy = effect();
if (destroy === void 0 || typeof destroy === "function") {
return destroy;
}
if (process.env.NODE_ENV !== "production") {
let message = "An effect function must not return anything besides a function, which is used for clean-up.";
if (destroy === null) {
message += " You returned 'null'. If your effect does not require clean-up, return 'undefined' (or nothing).";
} else if (typeof destroy.then === "function") {
message += "\n\nIt looks like you wrote 'useFocusEffect(async () => ...)' or returned a Promise. Instead, write the async function inside your effect and call it immediately:\n\nuseFocusEffect(\n React.useCallback(() => {\n async function fetchData() {\n // You can await here\n const response = await MyAPI.getData(someId);\n // ...\n }\n\n fetchData();\n }, [someId])\n);\n\nSee usage guide: https://reactnavigation.org/docs/use-focus-effect";
} else {
message += ` You returned '${JSON.stringify(destroy)}'.`;
}
console.error(message);
}
};
if (navigation.isFocused()) {
cleanup = callback();
isFocused = true;
}
const unsubscribeFocus = navigation.addListener("focus", () => {
if (isFocused) return;
cleanup?.();
cleanup = callback();
isFocused = true;
});
const unsubscribeBlur = navigation.addListener("blur", () => {
cleanup?.();
cleanup = void 0;
isFocused = false;
});
return () => {
cleanup?.();
unsubscribeFocus();
unsubscribeBlur();
};
}, [navigation, ...deps]);
}
export { useFocusEffect };
//# sourceMappingURL=useFocusEffect.mjs.map