[React] 7. Conditional Rendering


true && expression은 expression을 반환하고,
false && expression은 false를 반환하여 무시된다.
function Mailbox(props) {
  const unreadMessages = props.unreadMessages;
  return (
    <div>
      <h1>Hello!</h1>
      {unreadMessages.length > 0 &&
        <h2>
          You have {unreadMessages.length} unread messages.
        </h2>
      }
    </div>
  );
}

const messages = ['React', 'Re: React', 'Re:Re: React'];
ReactDOM.render(
  <Mailbox unreadMessages={messages} />,
  document.getElementById('root')
);
Inline If-Else with Conditional Operator
render() {
  const isLoggedIn = this.state.isLoggedIn;
  return (
    <div>
      {isLoggedIn ? (
        <LogoutButton onClick={this.handleLogoutClick} />
      ) : (
        <LoginButton onClick={this.handleLoginClick} />
      )}
    </div>
  );
}

댓글

이 블로그의 인기 게시물

[Django 공식문서 번역] REST Framework - Viewset and Router

Intel Open WebRTC Toolkit(OWT) Media server 설치하는법

[Django REST Framework] create() vs perform_create()