条件变量 === “something” &&

huangapple go评论85阅读模式
英文:

conditional variable === "something" &&

问题

为什么这个条件不起作用:

{
    selectedConfig === "house" ||
    selectedConfig === "Zone" ||
    selectedConfig === "Departament" &&
    <div>
        <div className="flex flex-col w-1/2">
            <InputForn 
                text="Nom"
                name="nom"
                inputType="text"
                value=""
            />
        </div>
        <div className="flex flex-col w-1/2">
            <InputForn 
                text="URL"
                name="url"
                inputType="text"
                value=""
            />
        </div>
    </div>
}

这是React.js,我正在创建一个条件来显示/隐藏一些组件,所以目前当我选择Departament时它能够显示,但是在条件为house or zone时不起作用。

我认为运算符||不起作用,为什么?或者我应该怎么做才能让它起作用?

英文:

why this coditional is not working:

   {
        selectedConfig === &quot;house&quot;      ||
        selectedConfig === &quot;Zone&quot;          ||
        selectedConfig === &quot;Departament&quot;  &amp;&amp;
        &lt;div&gt;
            &lt;div className=&quot;flex flex-col w-1/2&quot;&gt;
                &lt;InputForn 
                    text=&quot;Nom&quot;
                    name=&quot;nom&quot;
                    inputType=&quot;text&quot;
                    value=&quot;&quot;
                /&gt;
            &lt;/div&gt;
            &lt;div className=&quot;flex flex-col w-1/2&quot;&gt;
                &lt;InputForn 
                    text=&quot;URL &quot;
                    name=&quot;url&quot;
                    inputType=&quot;text&quot;
                    value=&quot;&quot;
                /&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    }

It's react js, I'm creating one conditional to show/hide some component, so currently It's showing when I select Departament but not work when conditional are house or zone

I think that operator || not work, why?? or what can I do to work it ???

答案1

得分: 2

(  selectedConfig === "house"
|| selectedConfig === "Zone"
|| selectedConfig === "Departament"
) && ...

The problem is that && has higher precedence than ||.

This means that

a || b || c && d

is equivalent to

a || b || (c && d)

But you want

(a || b || c) && d

As an aside, you say you want to check for zone, but you check for Zone. You are using inconsistent capitalization, and you misspelled "department". This is error-prone.

英文:

Add parens.

(  selectedConfig === &quot;house&quot;
|| selectedConfig === &quot;Zone&quot;
|| selectedConfig === &quot;Departament&quot;
) &amp;&amp; ...

The problem is that &amp;&amp; has higher precedence than ||.

This means that

a || b || c &amp;&amp; d

is equivalent to

a || b || ( c &amp;&amp; d )

But you want

( a || b || c ) &amp;&amp; d

As an aside, you say you want to check for zone, but you check for Zone. You are using inconsistent capitalization, and you misspelled "department". This is error prone.

答案2

得分: 0

需要使用括号对条件进行分组:

(selectedConfig === "house" || selectedConfig === "Zone" || selectedConfig === "Departament")
英文:

You need to group conditions with parentheses :

(selectedConfig === &quot;house&quot; || selectedConfig === &quot;Zone&quot; || selectedConfig === &quot;Departament&quot;)

huangapple
  • 本文由 发表于 2023年3月7日 23:13:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663781.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定