UNPKG

5.42 kBJavaScriptView Raw
1function _extends() { _extends = Object.assign || 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({
29 theme = DefaultTheme,
30 linking,
31 fallback = null,
32 documentTitle,
33 onReady,
34 ...rest
35}, ref) {
36 const isLinkingEnabled = linking ? linking.enabled !== false : false;
37
38 if (linking !== null && linking !== void 0 && linking.config) {
39 validatePathConfig(linking.config);
40 }
41
42 const refContainer = React.useRef(null);
43 useBackButton(refContainer);
44 useDocumentTitle(refContainer, documentTitle);
45 const {
46 getInitialState
47 } = useLinking(refContainer, {
48 independent: rest.independent,
49 enabled: isLinkingEnabled,
50 prefixes: [],
51 ...linking
52 }); // Add additional linking related info to the ref
53 // This will be used by the devtools
54
55 React.useEffect(() => {
56 if (refContainer.current) {
57 REACT_NAVIGATION_DEVTOOLS.set(refContainer.current, {
58 get linking() {
59 var _linking$prefixes, _linking$getStateFrom, _linking$getPathFromS, _linking$getActionFro;
60
61 return { ...linking,
62 enabled: isLinkingEnabled,
63 prefixes: (_linking$prefixes = linking === null || linking === void 0 ? void 0 : linking.prefixes) !== null && _linking$prefixes !== void 0 ? _linking$prefixes : [],
64 getStateFromPath: (_linking$getStateFrom = linking === null || linking === void 0 ? void 0 : linking.getStateFromPath) !== null && _linking$getStateFrom !== void 0 ? _linking$getStateFrom : getStateFromPath,
65 getPathFromState: (_linking$getPathFromS = linking === null || linking === void 0 ? void 0 : linking.getPathFromState) !== null && _linking$getPathFromS !== void 0 ? _linking$getPathFromS : getPathFromState,
66 getActionFromState: (_linking$getActionFro = linking === null || linking === void 0 ? void 0 : linking.getActionFromState) !== null && _linking$getActionFro !== void 0 ? _linking$getActionFro : getActionFromState
67 };
68 }
69
70 });
71 }
72 });
73 const [isResolved, initialState] = useThenable(getInitialState);
74 React.useImperativeHandle(ref, () => refContainer.current);
75 const linkingContext = React.useMemo(() => ({
76 options: linking
77 }), [linking]);
78 const isReady = rest.initialState != null || !isLinkingEnabled || isResolved;
79 const onReadyRef = React.useRef(onReady);
80 React.useEffect(() => {
81 onReadyRef.current = onReady;
82 });
83 React.useEffect(() => {
84 if (isReady) {
85 var _onReadyRef$current;
86
87 (_onReadyRef$current = onReadyRef.current) === null || _onReadyRef$current === void 0 ? void 0 : _onReadyRef$current.call(onReadyRef);
88 }
89 }, [isReady]);
90
91 if (!isReady) {
92 // This is temporary until we have Suspense for data-fetching
93 // Then the fallback will be handled by a parent `Suspense` component
94 return fallback;
95 }
96
97 return /*#__PURE__*/React.createElement(LinkingContext.Provider, {
98 value: linkingContext
99 }, /*#__PURE__*/React.createElement(ThemeProvider, {
100 value: theme
101 }, /*#__PURE__*/React.createElement(BaseNavigationContainer, _extends({}, rest, {
102 initialState: rest.initialState == null ? initialState : rest.initialState,
103 ref: refContainer
104 }))));
105}
106
107const NavigationContainer = /*#__PURE__*/React.forwardRef(NavigationContainerInner);
108export default NavigationContainer;
109//# sourceMappingURL=NavigationContainer.js.map
\No newline at end of file