import invariant from '../utils/invariant';
export default function getScreenForRouteName(routeConfigs, routeName) {
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 => `'${a}'`)
.join(',')}`
);
}
if (routeConfig.screen) {
// 如果目标路由项 routeConfig 上存在属性 screen,则认为找到了目标 screen
return routeConfig.screen;
}
if (typeof routeConfig.getScreen === 'function') {
// 如果目标路由项 routeConfig 上不存在属性 screen,
// 但是存在函数 getScreen() 并且其返回类型为 function, 则也认为找到了目标 screen
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;
}
return routeConfig;
}