Full Stack Development

Conditional Rendering

by Tomas Trescak t.trescak@westernsydney.edu.au

Conditional Rendering

/

Introduction

      import React from 'react';

function QuirkyAgeGreeter(props) {
  const { age } = props;

  if (age < 18) {
    return (
      <div>
        <h1>Hey there, little squirt!</h1>
        <p>Welcome to the wacky world of React!</p>
      </div>
    );
  } else {
    return (
      <div>
        <h1>Greetings, esteemed individual.</h1>
        <p>We're delighted to have you in our quirky React application.</p>
      </div>
    );
  }
}

function App() {
  return <div>
    <QuirkyAgeGreeter age={12} />
    <QuirkyAgeGreeter age={44} />
  </div>
}

render(<App />)
    

Javascript

/

Truthy and Falsy

Falsy Values

  • false - The boolean false .
  • 0 - The number zero.
  • -0 - The negative zero.
  • "" or '' or `` - Empty strings (single, double, or template literals).
  • null - The absence of any value.
  • undefined - A variable declared but not assigned a value.
  • NaN - Not-a-Number value.
Truthy Values

  • true - The boolean true.
  • Non-zero numbers (positive or negative, including floating points) - e.g., 1, -5, 3.14.
  • Non-empty strings - e.g., "Hello", 'false', " ".
  • Objects and arrays - e.g., {}, [].
  • Functions - Any function.
  • Infinity and -Infinity .
  • Symbol values.

Conditions

/

Ternary Operator

      console.log(undefined && "Boo")
console.log(8 && "Boo")
console.log(1 && "A" && 3 && 4)
console.log("Foo" && "Boo")

console.log(1 || "Boo")
console.log(0 || "Boo")
console.log("Foo" || "Boo")

console.log("A" || null && 0)

    

React Rendering

/

Truthy and Falsy

      const App = () => {
  return (
    <ul>
      <li><b>true</b>: {true}</li>
      <li><b>false:</b> {false}</li>
      <li><b>null:</b> {null}</li>
      <li><b>undefined:</b> {undefined}</li>
      <li><b>0:</b> {0}</li>
      <li><b>NaN:</b> {NaN}</li>
      <li><b>[]</b>: {[]}</li>
      <li><b>[1, "a", 3]</b>: {[1, "a", 3]}</li>
    </ul>
  );
};

render(<App />)
    

Conditions

/

Null Coalescing

      import React from 'react';

function UserProfile({ userName, userBio }) {
  const displayName = userName ?? 'Anonymous';
  const displayBio = userBio ?? 'No bio available';

  return (
    <div>
      <h1>Welcome, {displayName}!</h1>
      <p>{displayBio}</p>
    </div>
  );
}

// Usage
function App() {
  return (
    <div>
      <UserProfile 
        userName={undefined} 
        userBio={null} 
      />
      <UserProfile 
        userName="Tomas" 
        userBio="Jedi Forever" 
      />
    </div>
  )
}

render(<App />)
    

Javcascript

/

Ternary Operator

      console.log(true ? "Valeria" : "Tomas")

const a = 1000;
console.log(a < 500 ? "Profit" : "Loss") 
    

Conditions

/

Ternary Operator

      import React from 'react';

function UserProfile({ name, bio, age }) {
  return (
    <div>
      <h1>
        { age < 18 ? 'Yo, ': 'Welcome, ' }
        { 
          name 
          ? name 
          : <span style={{ color: 'red' }}>
              No User Name
            </span>
        }!
      </h1>
      <p>{
        bio 
        ? bio 
        : 'No bio available' }</p>
    </div>
  );
}

// Usage
function App() {
  return (
    <>
      <UserProfile 
        name="Tomas" 
        bio={null} 
        age={6} 
      />
      <hr />
      <UserProfile 
        name={null}
        bio="Invisible man" 
        age={44} 
      />
    </>
  )
}

render(<App />)
    

Conditions

/

Logical Chains

      console.log(null || "Tomas")
console.log(null && "Tomas")
console.log("Valeria" && "Tomas")
console.log("A" && "B" && "C")
console.log(null || false || "Tomas")
    

Conditions

/

Logical Chaining

      import React from 'react';

function UserProfile({ userName, userBio, userAge }) {
  return (
    <div>
      <h1>
        { userAge < 18 ? 'Yo, ': 'Welcome, ' }
        { userName || <span>[missing name]</span>}
      </h1>
      { userBio && <p>{userBio}</p> }
    </div>
  );
}

function App() {
  return <>
    <UserProfile userName={undefined} userBio={null} userAge={6} />
    <hr />
    <UserProfile userName="Tomas" userBio="Still Jedi" userAge={44} />
  </>
}

render(<App />)