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

119
Views
Refactor React UseEffect removing repeating else if

I have the following useEffect

I want to refactor it, removing all these else if lines

See the code

useEffect(() => {
    if(country === "WW") {
        setIsDisable(true);
    } else if (country === "GB") {
        setIsDisable(false);
    } else if (country === "DE") {
        setIsDisable(false);
    } else if (country === "FR") {
        setIsDisable(false);
    } else if (country === "BR") {
        setIsDisable(false);
    } else if (country === "US") {
        setIsDisable(false);
    } else if (country === "IT") {
        setIsDisable(false);
    } else if (country === "US") {
        setIsDisable(false);
    } else if (country === "ES") {
        setIsDisable(false);
    } else if (country === "MX") {
        setIsDisable(false);
    }
  }, [country]);

As you can see the country: GB, DE, FR, BR, US, IT, US, ES and MX sharing the same setIsDisable(false)

How would you write in 1 line of code whitout repeating the same else if ?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

useEffect(() => {
   setIsDisable(country === "WW");
}, [country]);

if you want to validate

useEffect(() => {
   if (country === "WW")
      setIsDisable(true);
   else if (["GB", "DE", "FR", "BR", "US", "IT", "ES", "MX"].includes(country))
      setIsDisable(false);
}, [country]);
about 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!