Rendering Components Conditionally

const UserGreeting = (props) => {
  return <h1>Welcome back!</h1>;
}

const GuestGreeting = (props) => {
  return <h1>Please sign up.</h1>;
}

const Greeting = (props) =>  {
  const isLoggedIn = props.isLoggedIn;
  if (isLoggedIn) {
    return <UserGreeting />;
  }
  return <GuestGreeting />;
}

ReactDOM.render(
  // Try changing to isLoggedIn={true}:
  <Greeting isLoggedIn={false} />,
  document.getElementById('root')
);

React Elements as Variables - Components

const LoginButton = (props) => {
  return (
    <button onClick={props.onClick}>
      Login
    </button>
  );
}

const LogoutButton = (props) => {
  return (
    <button onClick={props.onClick}>
      Logout
    </button>
  );
}

React Elements as Variables - Usage

function LoginControl (props) {

  const [isLoggedIn, setLogIn] = useState(false)

  handleLoginClick () {
    setLogIn(true);
  }

  handleLogoutClick () {
    setLogIn(false);
  }

  let button;

  if (isLoggedIn) {
    button = <LogoutButton onClick={ this.handleLogoutClick } />;
  } else {
    button = <LoginButton onClick={ this.handleLoginClick } />
  }

  return (
    <div>
      <Greeting isLoggedIn={ isLoggedIn } />
      { button }
    </div>
  );
}

Short Hand Logic Rendering

const 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')
);

Example CodePen

Inline Conditionals using Ternary

Simple

function Greeting (props) {

  const isLoggedIn = props.isLoggedIn;

  return (
    <div>
      The user is <b>{isLoggedIn ? 'currently' : 'not'}</b> logged in.
    </div>
  );
}

Complex

  const isLoggedIn = this.state.isLoggedIn;
  return (
    <div>
      {isLoggedIn ? (
        <LogoutButton onClick={this.handleLogoutClick} />
      ) : (
        <LoginButton onClick={this.handleLoginClick} />
      )}
    </div>
  );

Preventing Component Rendering

const WarningBanner = (props) => {
  if (!props.warn) {
    return null;
  }

  return (
    <div className="warning">
      Warning!
    </div>
  );
}

function Page (props) {

  const [showWarning, setWarning] = useState(true)

  function handleToggleClick() {
    if(showWarning) {
      setWarning(false)
    } else {
      setWarning(true)
    }
  }

  return (
    <div>
      <WarningBanner warn={showWarning} />
      <button onClick={handleToggleClick}>
        {showWarning ? 'Hide' : 'Show'}
      </button>
    </div>
  );
}

Example CodePen

Lab: Modal Display

Let's create a simple modal popup using the power of conditional rendering! Modal boxes are used all over the web, and are essentially a popup display. You see them quite often with image galleries where you can click a preview thumbnail, and the full size image opens as a popup.

 Previous Next 

Outline

[menu]

/