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

155
Views
Simplify javascript expression

i have this function in javascript:

export const commonRenderer = (option, useFormatter, hasSubLabel) => {
  if (useFormatter && hasSubLabel) {
    return (
      <React.Fragment>
        <FormattedMessage id={option.label} /><br /><FormattedMessage id={option.subLabel} />
      </React.Fragment>
    );
  }
  if (!useFormatter && hasSubLabel) {
    return (
      <React.Fragment>
        {option.label}<br />{option.subLabel}
      </React.Fragment>
    );
  }
  if (useFormatter && !hasSubLabel) {
    return (
      <FormattedMessage id={option.label} />
    );
  }
  return option.label;
};

and somehow i want to simplify this seems it looks really odd to me but im afraid of losing some cases. Any help?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Not sure if it's simpler or not, but you may try something like this:

export const commonRenderer = (option, useFormatter, hasSubLabel) => {
    const Element = useFormatter ? FormattedMessage : React.Fragment;
    const attr = useFormatter ? 'id' : 'children';

    return (
        <React.Fragment>
            <Element {...{ [attr]: option.label }} />
            {hasSubLabel && (
                <React.Fragment>
                    <br />
                    <Element {...{ [attr]: option.subLabel }} />
                </React.Fragment>
            )}
        </React.Fragment>
    );
};
about 4 years ago · Juan Pablo Isaza 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!