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

218
Views
Using different values in find method based on boolean value - React

I have a React component which accepts 3 props:

  • code (can be country or currency code)
  • currencyCode (boolean)
  • countryCode (boolean)

And makes use of a flagsList like so:

type CountryEntry = {
  currencyCode?: string,
  countryCode?: string,
  countryLabel: string,
  flagComponent: JSX.Element,
};

export const flagsList: CountryEntry[] = 
  {
    currencyCode: "AED",
    countryCode: "AE",
    countryLabel: "United Arab Emirates (the)",
    flagComponent: UnitedArabEmirates,
  },
  {
    currencyCode: "AFN",
    countryCode: "AF",
    countryLabel: "Afghanistan",
    flagComponent: Afghanistan,
  },
];

What I am trying to do is:

  • when currencyCode is true - search the flagsList by currencyCode, and
  • when countryCode is true search the flagsList by countryCode

whilst maintaining the fallback for each if a code cannot be found (StyledIcon)

const CountryFlag = ({ code, currencyCode, countryCode }: Props) => {
  const countryEntry = flagsList.find(
    (f) => f.currencyCode === currencyCode
  );

  if (!countryEntry) {
    return <StyledIcon name={"countryFallback"} />;
  } else {
    const FlagComponent = countryEntry.flagComponent;

    return (
      <StyledImageWrapper className={className}>
        <FlagComponent />
      </StyledImageWrapper>
    );
  }
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Consider using a string union or enum instead of multiple booleans.

type CountryFlagProps = {
  keyCode: string;
  keyType: "currencyCode" | "countryCode";
};

const CountryFlag = ({ keyCode, keyType }: CountryFlagProps) => {
  const countryEntry = flagsList.find((f) => f[keyType] === keyCode);

  if (countryEntry) {
    return countryEntry.flagComponent
  }

  // Choose your fallback
  return <Flag country="UK" />;
};

Edit crazy-parm-hex0l


If you know all of your codes in advance you can be more restrictive and consumers will get better intellisense when using <CountryFlag />

type CurrencyCode = "AED" | "AFN";
type CountryCode = "AE" | "AF";

type CountryEntry = {
  currencyCode?: CurrencyCode;
  countryCode?: CountryCode;
  countryLabel: string;
  flagComponent: JSX.Element;
};

const flagsList: Array<CountryEntry> = [
  {
    currencyCode: "AED",
    countryCode: "AE",
    countryLabel: "United Arab Emirates (the)",
    flagComponent: <Flag country="AE" />
  },
  {
    currencyCode: "AFN",
    countryCode: "AF",
    countryLabel: "Afghanistan",
    flagComponent: <Flag country="AF" />
  }
];

type CountryFlagProps = {
  keyCode: CurrencyCode | CountryCode;
  keyType: "currencyCode" | "countryCode";
};

const CountryFlag = ({ keyCode, keyType }: CountryFlagProps) => {
  const countryEntry = flagsList.find((f) => f[keyType] === keyCode);

  if (countryEntry) {
    return countryEntry.flagComponent;
  }

  return <Flag country="UK" />;
};

export default function App() {
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <CountryFlag keyCode="AF" keyType="countryCode" />
    </div>
  );
}

Edit amazing-ardinghelli-ocuk4

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!