简体   繁体   English

反应组件的嵌套三元运算符

[英]Nested ternary operator for react components

I want to come up with a nested if ternary operator with react components but am being challenged, this is my logic:我想提出一个带有反应组件的嵌套 if 三元运算符,但受到挑战,这是我的逻辑:

if (value==='bank' && is_valid_iban && is_completed) {

return <Checked/>

}

else if (is_completed) {

return <Checked/>
}

else if (value==='businessplan' && is_required) {

return <NotChecked/>
}

This was my change:这是我的改变:

{
(!value=== 'bank' || is_valid_iban) &&
                is_completed ? (<Checked/>) : (value ==='businessplan' && is_required && (<NotChecked/>))

}

What could be the best way of coming up with a ternary operator for the logic above.为上述逻辑提出三元运算符的最佳方法可能是什么。

Thanks谢谢

This solution seeks to simplify the below given set of conditions:该解决方案旨在简化以下给定的一组条件:

if (value==='bank' && is_valid_iban && is_completed) {
  return <Checked/>
} else if (is_completed) {
  return <Checked/>
} else if (value==='businessplan' && is_required) {
  return <NotChecked/>
}

It is observed from the above that is_completed is part of the first & second conditions.从上面观察到, is_completed是第一个和第二个条件的一部分。 Something like this: ((A && B && C) || C) which can be represented as (A && B) || C像这样: ((A && B && C) || C)可以表示为(A && B) || C (A && B) || C . (A && B) || C

Using a standard if..else structure:使用标准的if..else结构:

if ((value === 'bank' && is_valid_iban) || is_completed) return <Checked />
else if (value === 'businessplan' && is_required) return <NotChecked />
else return null;

Using ternary ?:使用三元?:

return (
  ((value === 'bank' && is_valid_iban) || is_completed)
  ? <Checked />
  : (value === 'businessplan' && is_required)
    ? <NotChecked />
    : null
);

When using ?: please always indent the code so it is readable.使用?:时,请始终缩进代码以使其易于阅读。 Same applies for if else as well;同样适用于if else however, the if else structure is a lot more readable than ?: .但是, if else结构比?:可读性强得多。

Make it Simple:让它变得简单:

  {(value === "bank" && is_valid_iban && is_completed) || is_completed ? (
    <Checked />
  ) : (value === "businessplan" && is_required) ? (
    <NotChecked />
  ) : (
    <NotChecked />
  )}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM