UNPKG

@react-navigation/core

Version:

Core utilities for building navigators

88 lines (87 loc) 3.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useNavigationHelpers = useNavigationHelpers; var _routers = require("@react-navigation/routers"); var React = _interopRequireWildcard(require("react")); var _NavigationContext = require("./NavigationContext.js"); var _types = require("./types.js"); var _UnhandledActionContext = require("./UnhandledActionContext.js"); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } // This is to make TypeScript compiler happy // eslint-disable-next-line @typescript-eslint/no-unused-expressions _types.PrivateValueStore; /** * Navigation object with helper methods to be used by a navigator. * This object includes methods for common actions as well as methods the parent screen's navigation object. */ function useNavigationHelpers({ id: navigatorId, onAction, getState, emitter, router, stateRef }) { const onUnhandledAction = React.useContext(_UnhandledActionContext.UnhandledActionContext); const parentNavigationHelpers = React.useContext(_NavigationContext.NavigationContext); return React.useMemo(() => { const dispatch = op => { const action = typeof op === 'function' ? op(getState()) : op; const handled = onAction(action); if (!handled) { onUnhandledAction?.(action); } }; const actions = { ...router.actionCreators, ..._routers.CommonActions }; const helpers = Object.keys(actions).reduce((acc, name) => { // @ts-expect-error: name is a valid key, but TypeScript is dumb acc[name] = (...args) => dispatch(actions[name](...args)); return acc; }, {}); const navigationHelpers = { ...parentNavigationHelpers, ...helpers, dispatch, emit: emitter.emit, isFocused: parentNavigationHelpers ? parentNavigationHelpers.isFocused : () => true, canGoBack: () => { const state = getState(); return router.getStateForAction(state, _routers.CommonActions.goBack(), { routeNames: state.routeNames, routeParamList: {}, routeGetIdList: {} }) !== null || parentNavigationHelpers?.canGoBack() || false; }, getId: () => navigatorId, getParent: id => { if (id !== undefined) { let current = navigationHelpers; while (current && id !== current.getId()) { current = current.getParent(); } return current; } return parentNavigationHelpers; }, getState: () => { // FIXME: Workaround for when the state is read during render // By this time, we haven't committed the new state yet // Without this `useSyncExternalStore` will keep reading the old state // This may result in `useNavigationState` or `useIsFocused` returning wrong values // Apart from `useSyncExternalStore`, `getState` should never be called during render if (stateRef.current != null) { return stateRef.current; } return getState(); } }; return navigationHelpers; }, [router, parentNavigationHelpers, emitter.emit, getState, onAction, onUnhandledAction, navigatorId, stateRef]); } //# sourceMappingURL=useNavigationHelpers.js.map