gpt4 book ai didi

react-native - React Native - 切换两个值

转载 作者:行者123 更新时间:2023-12-04 09:45:41 25 4
gpt4 key购买 nike

我正在尝试查看是否可以在导航器的两个值之间切换:
这有效:

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;
}

但它不起作用。
任何的想法?
谢谢

最佳答案

由于您对每条路线都有特定的颜色,因此您可以将其设置为对象并从 switch 返回。并且 switch 不支持多个变量。您将不得不使用 if else。而不是返回外面,您可以使用带有默认颜色的默认情况返回。

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' };
}

}

关于react-native - React Native - 切换两个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62128985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com