CodeBlanks Logo Icon CodeBlanks

Summary - Conditional rendering

Write a banner that shows a different heading for logged-in and guest users.

Summary - Conditional rendering

In this group you learned conditional rendering, the ternary operator, the logical AND pattern, and early return. Now it's time to combine them.

Use ternary for two options, && for one-sided showing, and early return for edge cases. These three cover most conditional rendering needs.

In the next step you'll write a banner that shows a different heading for logged-in and guest users.

Practice Banner two states

Complete the skeleton. Write a component called Banner that takes ({ '{ isLoggedIn }' }) as parameter. If isLoggedIn is true, return <h1>Welcome</h1>; otherwise <h1>Please log in</h1> (with ternary). Don't forget to export.

Starter code

App.jsx
// Write the Banner component here

Sample solution

App.jsx
function Banner({ isLoggedIn }) {
  return isLoggedIn ? <h1>Welcome</h1> : <h1>Please log in</h1>;
}

export default Banner;

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which is best for two options?

  • Ternary Correct answer
  • &&
  • Early return
  • if statement

Explanation: Ternary is ideal for two options.

2 Match the conditional render methods

Term Meaning
Ternary Two options
&& One-sided
Early return Edge case
null Not rendered

All lessons in Beginner