Search code examples
reactjsconditional-operatorconditional-rendering

Multiple Inline If-Else with Conditional Operator


Can you give me a hand with this expression . Basically what i am trying to say is If props.row.registered is true set disabled to be true or if is props.row.registered is undefined set it to false.

<Button
     disabled={!props.row.registered ? true : !props.row.registered === undefined ? false : true}
    ...
    />

Solution

  • Your ternary operator is basically doing this:

    if(props.row.registered === true) {
      return true;
    else {
      return false;
    }
    

    which can be simplified to:

    return props.row.registered;
    

    So for your conditions it would be:

    props.row.registered || props.row.registered !== undefined
    

    (based on your statement since your code it's setting disabled to true if props.row.registered is false, which is the opposite of your statement)