UNPKG

1.28 kBJavaScriptView Raw
1import * as React from 'react';
2const MULTIPLE_NAVIGATOR_ERROR = `Another navigator is already registered for this container. You likely have multiple navigators under a single "NavigationContainer" or "Screen". Make sure each navigator is under a separate "Screen" container. See https://reactnavigation.org/docs/nesting-navigators for a guide on nesting.`;
3export const SingleNavigatorContext = /*#__PURE__*/React.createContext(undefined);
4/**
5 * Component which ensures that there's only one navigator nested under it.
6 */
7
8export default function EnsureSingleNavigator(_ref) {
9 let {
10 children
11 } = _ref;
12 const navigatorKeyRef = React.useRef();
13 const value = React.useMemo(() => ({
14 register(key) {
15 const currentKey = navigatorKeyRef.current;
16
17 if (currentKey !== undefined && key !== currentKey) {
18 throw new Error(MULTIPLE_NAVIGATOR_ERROR);
19 }
20
21 navigatorKeyRef.current = key;
22 },
23
24 unregister(key) {
25 const currentKey = navigatorKeyRef.current;
26
27 if (key !== currentKey) {
28 return;
29 }
30
31 navigatorKeyRef.current = undefined;
32 }
33
34 }), []);
35 return /*#__PURE__*/React.createElement(SingleNavigatorContext.Provider, {
36 value: value
37 }, children);
38}
39//# sourceMappingURL=EnsureSingleNavigator.js.map
\No newline at end of file