skip to Main Content

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 ???

2

Answers


  1. You need to group conditions with parentheses :

    (selectedConfig === "house" || selectedConfig === "Zone" || selectedConfig === "Departament")
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search