Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

243
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda