4

First time trying this so please bare with me.

I want to return some elements based on one of three conditions. Currently the code works for two conditions:

<h1 id="largeh1">
   {isEnvironmentBFE
     ? outputEnFr(
       "BFE Environment English Text",
       "BFE Environment French Text",
       this.props.lang
       )
     : outputEnFr(
        "Acme Environment English Text",
       "Acme Environment French Text",
       this.props.lang
     )
    // Want third condition here
    }
</h1>

I'd like to add one more condition for if the environment is "isEnvironmentBFE_OR_BFERENTAL" - so when people hit this third environment, it gives them different English / French values. Would like to stick with a ternary operator if possible.

Any idea how I can add a third condition in here?

3
  • You can use something like this isEnvironmentBFE_OR_BFERENTAL ? 'New component' : (isEnvironmentBFE ? outputEnFr() : outputEnFr()) Commented Feb 22, 2021 at 15:17
  • it would be better to abstract the logic to a helper function instead. your template would get messy easily Commented Feb 22, 2021 at 15:20
  • 1
    Nesting ternaries: the tool of choice to quickly produce unclear bug-ridden code Commented Feb 22, 2021 at 15:30

3 Answers 3

7
<h1 id="largeh1">
   {
     isEnvironmentBFE
     ? (
         outputEnFr(
           "BFE Environment English Text",
           "BFE Environment French Text",
           this.props.lang
         )
     ) : NEW CONDITIONAL HERE
     ? (NEW RESULT)
     : (
         outputEnFr(
           "Acme Environment English Text",
           "Acme Environment French Text",
           this.props.lang
         )
     )
   }
</h1>

This is equivalent to

if(isEnvironmentBFE)
  //yadda
else if (foo)
  //yadda yadda
else 
  //yadda
Sign up to request clarification or add additional context in comments.

1 Comment

instead of else if I want if in the middle then?
3

The most readable way to go about this is by moving that logic out of your return statement and creating a function for it which handles that logic. You can do this within the same component, or extract that function to a different file if it should be reusable on another component:

const Component = () => {

  const renderText = () => {
    if (...) {
      return ...
    } else if (...) {
      return ...
    }

    return ...
  }

  return (
    <h1>
      renderText();
    <h1>
  )
};

If you would like to stick with a ternary you can do:

return condition1 ? (condition2 ? result1 : result2) : result3 

Depends on your personal preference, although I suggest using an if-statement or a switch-statement for anything that has more than 1 condition as it is easier to read and comprehend what is happening.

Comments

1

Ternary is useful to produce concise code for a simple conditions to switch between two outcomes. But I advise against nesting it. There is a nice React idiom that would make more sense here:

<div>
  { condition1 && (<div>foo</div>)}
  { condition2 && (<div>bar</div>)}
  { condition3 && (<div>buz</div>)}
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.