skip to Main Content

Please see below code ternary operator is not working in react inside jsx with value from context api

const { isSortex, isTransportLocal, setAcessLevel, isTransportOutside } = useContext(accessLevel);

//isTransportOutside is false and 
//isTransportLocal is true
return (
    <>
        <div className="R3-my-Blue-dark" style={style}>
            <PannelButtonContainer>
                {isTransportOutside ? <PannelButton buttontext="Truck Freight Outside" /> : null}
                {isTransportLocal ? <PannelButton buttontext="Workslip" /> : null}
            </PannelButtonContainer>
        </div>
    </>
);

both component are rendering while true should render and false should not

2

Answers


  1. You should use && instead of ? for conditional rendering and try to not use null.

    For example

    { isTransportOutside && <PannelButton buttontext="Truck Freight Outside" /> }
    
    Login or Signup to reply.
  2. try using strict comparison ie with === true

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