UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

48 lines (43 loc) 1.94 kB
import { useEffect } from "react"; import { useOptionalNavigation } from "./link/useLoadedNavigation.native.js"; function useFocusEffect(effect, args) { var navigation = useOptionalNavigation(); useEffect(function () { if (navigation) { var isFocused = !1, cleanup, callback = function () { var destroy = effect(); if (destroy === void 0 || typeof destroy == "function") return destroy; if (process.env.NODE_ENV !== "production") { var message = "An effect function must not return anything besides a function, which is used for clean-up."; destroy === null ? message += " You returned 'null'. If your effect does not require clean-up, return 'undefined' (or nothing)." : typeof destroy.then == "function" ? message += ` It looks like you wrote 'useFocusEffect(async () => ...)' or returned a Promise. Instead, write the async function inside your effect and call it immediately: useFocusEffect( React.useCallback(() => { async function fetchData() { // You can await here const response = await MyAPI.getData(someId); // ... } fetchData(); }, [someId]) ); See usage guide: https://reactnavigation.org/docs/use-focus-effect` : message += ` You returned '${JSON.stringify(destroy)}'.`, console.error(message); } }; navigation.isFocused() && (cleanup = callback(), isFocused = !0); var unsubscribeFocus = navigation.addListener("focus", function () { isFocused || (cleanup?.(), cleanup = callback(), isFocused = !0); }), unsubscribeBlur = navigation.addListener("blur", function () { cleanup?.(), cleanup = void 0, isFocused = !1; }); return function () { cleanup?.(), unsubscribeFocus(), unsubscribeBlur(); }; } }, [navigation, ...args]); } export { useFocusEffect }; //# sourceMappingURL=useFocusEffect.native.js.map