UNPKG

@react-navigation/core

Version:

Core utilities for building navigators

64 lines (62 loc) 2.44 kB
import * as React from 'react'; import NavigationBuilderContext from './NavigationBuilderContext'; import NavigationRouteContext from './NavigationRouteContext'; const VISITED_ROUTE_KEYS = Symbol('VISITED_ROUTE_KEYS'); export const shouldPreventRemove = (emitter, beforeRemoveListeners, currentRoutes, nextRoutes, action) => { const nextRouteKeys = nextRoutes.map(route => route.key); // Call these in reverse order so last screens handle the event first const removedRoutes = currentRoutes.filter(route => !nextRouteKeys.includes(route.key)).reverse(); const visitedRouteKeys = // @ts-expect-error: add this property to mark that we've already emitted this action action[VISITED_ROUTE_KEYS] ?? new Set(); const beforeRemoveAction = { ...action, [VISITED_ROUTE_KEYS]: visitedRouteKeys }; for (const route of removedRoutes) { var _beforeRemoveListener; if (visitedRouteKeys.has(route.key)) { // Skip if we've already emitted this action for this screen continue; } // First, we need to check if any child screens want to prevent it const isPrevented = (_beforeRemoveListener = beforeRemoveListeners[route.key]) === null || _beforeRemoveListener === void 0 ? void 0 : _beforeRemoveListener.call(beforeRemoveListeners, beforeRemoveAction); if (isPrevented) { return true; } visitedRouteKeys.add(route.key); const event = emitter.emit({ type: 'beforeRemove', target: route.key, data: { action: beforeRemoveAction }, canPreventDefault: true }); if (event.defaultPrevented) { return true; } } return false; }; export default function useOnPreventRemove(_ref) { let { getState, emitter, beforeRemoveListeners } = _ref; const { addKeyedListener } = React.useContext(NavigationBuilderContext); const route = React.useContext(NavigationRouteContext); const routeKey = route === null || route === void 0 ? void 0 : route.key; React.useEffect(() => { if (routeKey) { return addKeyedListener === null || addKeyedListener === void 0 ? void 0 : addKeyedListener('beforeRemove', routeKey, action => { const state = getState(); return shouldPreventRemove(emitter, beforeRemoveListeners, state.routes, [], action); }); } }, [addKeyedListener, beforeRemoveListeners, emitter, getState, routeKey]); } //# sourceMappingURL=useOnPreventRemove.js.map