Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

249
Vistas
Support optional arguments in switch statement in React component

From the api I can query:

  • type which returns strings 'Type A' or 'Type B'
  • typeXy which returns strings 'Type X' or 'Type Y'

Based on those values I want to create a switch statement to get the proper value.

const getLabel = (type?: string, typeXy?: string) => {
    switch (type || typeXy) {
      case 'Type A':
        return labelA;
      case 'Type B':
        return labelB;
      case 'Type X':
        return labelX;
      case 'Type Y':
        return labelY;
      default:
        return '';
    }
  };

Then in my React component I want to pass the function getLabel() through the label prop to send the correct label to the child component.

  return (
    <Grid
      items={data?.items?.map((node) => ({
        label: getLabel(node.type || node.typeXy),
      }))}
    />
  );

But how do I create the switch statement which accepts or argument/field A or argument/field B?

And then in the function it can accepts or field A or field B (node.type or node.typeXy)?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Try this (in vanilla javascript)

const getLabel = (type, typeXy) => {
    switch (type || typeXy) {
      case 'Type A':
        return 'labelA';
      case 'Type B':
        return 'labelB';
      case 'Type X':
        return 'labelX';
      case 'Type Y':
        return 'labelY';
      default:
        return '';
    }
  };
  
console.log(getLabel('', 'Type Y'))
console.log(getLabel('Type A', ''))

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda