1 | import { nanoid } from 'nanoid/non-secure';
|
2 | import * as React from 'react';
|
3 |
|
4 | import { SingleNavigatorContext } from './EnsureSingleNavigator';
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | export default function useRegisterNavigator() {
|
11 | const [key] = React.useState(() => nanoid());
|
12 | const container = React.useContext(SingleNavigatorContext);
|
13 |
|
14 | if (container === undefined) {
|
15 | throw new Error(
|
16 | "Couldn't register the navigator. Have you wrapped your app with 'NavigationContainer'?\n\nThis can also happen if there are multiple copies of '@react-navigation' packages installed."
|
17 | );
|
18 | }
|
19 |
|
20 | React.useEffect(() => {
|
21 | const { register, unregister } = container;
|
22 |
|
23 | register(key);
|
24 |
|
25 | return () => unregister(key);
|
26 | }, [container, key]);
|
27 |
|
28 | return key;
|
29 | }
|