Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!