2

I'm trying to see if I can switch between two values for my navigator: This works:

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

where I need I exec:

getHeaderTitle(route) // works

and I would like to add a value (color) to avoid to create a second one just for it:

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

But it doesn't work. Any idea? thanks

Marco Disco
  • 527
  • 7
  • 22
  • 1
    Probably I would return an object in this case like `return { routeName, color }` instead. Also in other cases once you have more than 1 variable. [Return multiple values in JavaScript](https://stackoverflow.com/questions/2917175/return-multiple-values-in-javascript) will explain more about the topic. – norbitrial Jun 01 '20 at 09:12

1 Answers1

2

As you have specific colors for each route, you can set it as an object and return it from switch. And switch doesnt support multiple variables. You will have to go with an if else. And rather than returning outside you can return using the default case with a default 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' };
  }

}
Guruparan Giritharan
  • 15,660
  • 4
  • 27
  • 50