one
Version:
One is a new React Framework that makes Vite serve both native and web.
52 lines (51 loc) • 2.18 kB
JavaScript
import { useNavigation } from "@react-navigation/native";
import * as React from "react";
function useBlocker(shouldBlock) {
var navigation = useNavigation(),
[state, setState] = React.useState("unblocked"),
[pendingEvent, setPendingEvent] = React.useState(null),
[blockedLocation, setBlockedLocation] = React.useState(null),
shouldBlockRef = React.useRef(shouldBlock);
shouldBlockRef.current = shouldBlock, React.useEffect(function () {
var unsubscribe = navigation.addListener("beforeRemove", function (e) {
var _e_data_action,
_e_data,
currentShouldBlock = shouldBlockRef.current,
payload = (_e_data = e.data) === null || _e_data === void 0 || (_e_data_action = _e_data.action) === null || _e_data_action === void 0 ? void 0 : _e_data_action.payload,
nextLocation = payload?.name || "previous screen",
block = typeof currentShouldBlock == "function" ? currentShouldBlock({
currentLocation: "",
// Not easily available on native
nextLocation,
historyAction: "pop"
}) : currentShouldBlock;
block && (e.preventDefault(), setPendingEvent(e), setBlockedLocation(nextLocation), setState("blocked"));
});
return unsubscribe;
}, [navigation]);
var reset = React.useCallback(function () {
setPendingEvent(null), setBlockedLocation(null), setState("unblocked");
}, []),
proceed = React.useCallback(function () {
pendingEvent && (setState("proceeding"), navigation.dispatch(pendingEvent.data.action), setTimeout(function () {
setPendingEvent(null), setBlockedLocation(null), setState("unblocked");
}, 100));
}, [navigation, pendingEvent]);
return state === "unblocked" ? {
state: "unblocked"
} : state === "proceeding" ? {
state: "proceeding",
location: blockedLocation
} : {
state: "blocked",
reset,
proceed,
location: blockedLocation
};
}
function checkBlocker(_nextLocation) {
var _historyAction = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "push";
return !1;
}
export { checkBlocker, useBlocker };
//# sourceMappingURL=useBlocker.native.js.map