React native React Native-切换两个值
我正在尝试查看是否可以在导航器的两个值之间切换: 这项工作:React native React Native-切换两个值,react-native,React Native,我正在尝试查看是否可以在导航器的两个值之间切换: 这项工作: function getHeaderTitle(route) { const routeName = route.state ? route.state.routes[route.state.index].name : route.params?.screen || 'The Market'; switch (routeName) { case 'Stands': return 'The
function getHeaderTitle(route) {
const routeName = route.state
? route.state.routes[route.state.index].name
: route.params?.screen || 'The Market';
switch (routeName) {
case 'Stands':
return 'The Market';
case 'Map':
return 'How to find the Market';
case 'Favorites':
return 'Favorites';
case 'Info':
return 'Info about the Market';
}
return routeName;
}
在我需要的地方,我执行:
getHeaderTitle(route) // works
我想添加一个值(颜色),以避免仅为其创建第二个值:
function getHeaderTitle(route) {
const routeName = route.state
? route.state.routes[route.state.index].name
: route.params?.screen || 'The Market';
let color;
switch ((routeName, color)) {
case 'Stands':
return 'The Market', (color = Colors.MarketColor);
case 'Map':
return 'How to find the Market', (color = Colors.MapColor);
case 'Favorites':
return 'Favorites', (color = Colors.FavColor);
case 'Info':
return 'Info about the Market', (color = Colors.InfoColor);
}
return routeName, color;
}
但它不起作用。
有什么想法吗?
谢谢因为每个路线都有特定的颜色,所以可以将其设置为对象并从交换机返回。开关不支持多个变量。你必须用“如果没有”的话。您可以使用带有默认颜色的默认大小写返回,而不是返回外部
function getHeaderTitle(route) {
const routeName = route.state
? route.state.routes[route.state.index].name
: route.params ?.screen || 'The Market';
switch (routeName) {
case 'Stands':
return { route: 'The Market', color: Colors.MarketColor };
case 'Map':
return { route: 'How to find the Market', color: Colors.MapColor };
case 'Favorites':
return { route: 'Favorites', color: Colors.FavColor };
case 'Info':
return { route: 'Info about the Market', color: Colors.InfoColor };
default:
return { route: routeName, color: 'default color' };
}
}
在这种情况下,我可能会返回一个对象,比如
return{routeName,color}
。另外,在其他情况下,当您有一个以上的变量时。将解释更多关于该主题的内容。谢谢,看起来很棒!但奇怪的是,我得到的对象无效,因为react子级使用数组而不是这将返回一个对象,您必须使用一个属性、路由或颜色,如const result=getheadertile('Market');结果:1。我学到了一些新东西,但不管用。它不能改变我需要的方式