UNPKG

5.48 kBJavaScriptView Raw
1function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
3import { BaseNavigationContainer, getActionFromState, getPathFromState, getStateFromPath, validatePathConfig } from '@react-navigation/core';
4import * as React from 'react';
5import LinkingContext from './LinkingContext';
6import DefaultTheme from './theming/DefaultTheme';
7import ThemeProvider from './theming/ThemeProvider';
8import useBackButton from './useBackButton';
9import useDocumentTitle from './useDocumentTitle';
10import useLinking from './useLinking';
11import useThenable from './useThenable';
12global.REACT_NAVIGATION_DEVTOOLS = new WeakMap();
13
14/**
15 * Container component which holds the navigation state designed for React Native apps.
16 * This should be rendered at the root wrapping the whole app.
17 *
18 * @param props.initialState Initial state object for the navigation tree. When deep link handling is enabled, this will override deep links when specified. Make sure that you don't specify an `initialState` when there's a deep link (`Linking.getInitialURL()`).
19 * @param props.onReady Callback which is called after the navigation tree mounts.
20 * @param props.onStateChange Callback which is called with the latest navigation state when it changes.
21 * @param props.theme Theme object for the navigators.
22 * @param props.linking Options for deep linking. Deep link handling is enabled when this prop is provided, unless `linking.enabled` is `false`.
23 * @param props.fallback Fallback component to render until we have finished getting initial state when linking is enabled. Defaults to `null`.
24 * @param props.documentTitle Options to configure the document title on Web. Updating document title is handled by default unless `documentTitle.enabled` is `false`.
25 * @param props.children Child elements to render the content.
26 * @param props.ref Ref object which refers to the navigation object containing helper methods.
27 */
28function NavigationContainerInner(_ref, ref) {
29 let {
30 theme = DefaultTheme,
31 linking,
32 fallback = null,
33 documentTitle,
34 onReady,
35 ...rest
36 } = _ref;
37 const isLinkingEnabled = linking ? linking.enabled !== false : false;
38
39 if (linking !== null && linking !== void 0 && linking.config) {
40 validatePathConfig(linking.config);
41 }
42
43 const refContainer = React.useRef(null);
44 useBackButton(refContainer);
45 useDocumentTitle(refContainer, documentTitle);
46 const {
47 getInitialState
48 } = useLinking(refContainer, {
49 independent: rest.independent,
50 enabled: isLinkingEnabled,
51 prefixes: [],
52 ...linking
53 }); // Add additional linking related info to the ref
54 // This will be used by the devtools
55
56 React.useEffect(() => {
57 if (refContainer.current) {
58 REACT_NAVIGATION_DEVTOOLS.set(refContainer.current, {
59 get linking() {
60 var _linking$prefixes, _linking$getStateFrom, _linking$getPathFromS, _linking$getActionFro;
61
62 return { ...linking,
63 enabled: isLinkingEnabled,
64 prefixes: (_linking$prefixes = linking === null || linking === void 0 ? void 0 : linking.prefixes) !== null && _linking$prefixes !== void 0 ? _linking$prefixes : [],
65 getStateFromPath: (_linking$getStateFrom = linking === null || linking === void 0 ? void 0 : linking.getStateFromPath) !== null && _linking$getStateFrom !== void 0 ? _linking$getStateFrom : getStateFromPath,
66 getPathFromState: (_linking$getPathFromS = linking === null || linking === void 0 ? void 0 : linking.getPathFromState) !== null && _linking$getPathFromS !== void 0 ? _linking$getPathFromS : getPathFromState,
67 getActionFromState: (_linking$getActionFro = linking === null || linking === void 0 ? void 0 : linking.getActionFromState) !== null && _linking$getActionFro !== void 0 ? _linking$getActionFro : getActionFromState
68 };
69 }
70
71 });
72 }
73 });
74 const [isResolved, initialState] = useThenable(getInitialState);
75 React.useImperativeHandle(ref, () => refContainer.current);
76 const linkingContext = React.useMemo(() => ({
77 options: linking
78 }), [linking]);
79 const isReady = rest.initialState != null || !isLinkingEnabled || isResolved;
80 const onReadyRef = React.useRef(onReady);
81 React.useEffect(() => {
82 onReadyRef.current = onReady;
83 });
84 React.useEffect(() => {
85 if (isReady) {
86 var _onReadyRef$current;
87
88 (_onReadyRef$current = onReadyRef.current) === null || _onReadyRef$current === void 0 ? void 0 : _onReadyRef$current.call(onReadyRef);
89 }
90 }, [isReady]);
91
92 if (!isReady) {
93 // This is temporary until we have Suspense for data-fetching
94 // Then the fallback will be handled by a parent `Suspense` component
95 return fallback;
96 }
97
98 return /*#__PURE__*/React.createElement(LinkingContext.Provider, {
99 value: linkingContext
100 }, /*#__PURE__*/React.createElement(ThemeProvider, {
101 value: theme
102 }, /*#__PURE__*/React.createElement(BaseNavigationContainer, _extends({}, rest, {
103 initialState: rest.initialState == null ? initialState : rest.initialState,
104 ref: refContainer
105 }))));
106}
107
108const NavigationContainer = /*#__PURE__*/React.forwardRef(NavigationContainerInner);
109export default NavigationContainer;
110//# sourceMappingURL=NavigationContainer.js.map
\No newline at end of file