어쩌다 알게 된 ƪ(•̃͡•̃͡ ƪ

🟦 [React] && 연산자 / React에서 if 문 대신 && 연산자 사용하기 본문

개발/🟦 React

🟦 [React] && 연산자 / React에서 if 문 대신 && 연산자 사용하기

비니_ 2025. 4. 1. 19:46
728x90

 

 

 

이런 코드를 만났따!

{(company.get.isAdmin || (user.name != 'A' && user.name != 'B')) &&
    <div>
        <UserProfile />
    </div>
}

 

=> company.get.isAdmin이 true거나, user.name이 A와 B가 아닐 때 <div> <UserProfile /> </div>를 보여라

 

=> 리액트에선JSX안에서 if문을 직접 쓸 수 없기 때문에 && 연산자로 기능 구현

 

👉 A && B => A가 무조건 참이여야 B를 탄다. 그러므로 A 조건이 true면 B를 보이게 하는 코드!

 

 

=> 삼항연산자로도 가능하지만 && 사용하는 것이 더 간결!

{(company.get.isAdmin || (user.name !== 'A' && user.name !== 'B')) 
    ? <div><UserProfile /></div> 
    : null
 }

 

 

 

728x90
Comments