getScreenForRouteName.js.flow
1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* @flow */
import invariant from '../utils/invariant';
import type {
NavigationComponent,
NavigationRouteConfigMap,
} from '../TypeDefinition';
/**
* Simple helper that gets a single screen (React component or navigator)
* out of the navigator config.
*/
export default function getScreenForRouteName(
routeConfigs: NavigationRouteConfigMap,
routeName: string
): NavigationComponent {
const routeConfig = routeConfigs[routeName];
if (!routeConfig) {
throw new Error(
`There is no route defined for key ${routeName}.\n` +
`Must be one of: ${Object.keys(routeConfigs)
.map((a: string) => `'${a}'`)
.join(',')}`
);
}
if (routeConfig.screen) {
return routeConfig.screen;
}
if (typeof routeConfig.getScreen === 'function') {
const screen = routeConfig.getScreen();
invariant(
typeof screen === 'function',
`The getScreen defined for route '${routeName} didn't return a valid ` +
'screen or navigator.\n\n' +
'Please pass it like this:\n' +
`${routeName}: {\n getScreen: () => require('./MyScreen').default\n}`
);
return screen;
}
throw new Error(`Route ${routeName} must define a screen or a getScreen.`);
}