英文:
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 === "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>
}
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 === "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.
答案2
得分: 0
需要使用括号对条件进行分组:
(selectedConfig === "house" || selectedConfig === "Zone" || selectedConfig === "Departament")
英文:
You need to group conditions with parentheses :
(selectedConfig === "house" || selectedConfig === "Zone" || selectedConfig === "Departament")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论