React · Chapter 4 of 26

Conditional rendering

By the end of this chapter you can make a component show different things for different data: with if and an early return, with ? : inside JSX, with && for "this or nothing", and with null for nothing at all. You can choose a class name or a label the same way, say what React keeps on the page when the choice changes, and avoid the stray 0 that && puts on a page. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Thinking in components
  • 4 tasks
  • React
  • In the all labs plan

The first 2 chapters of React are free. This one comes with the all labs plan.

Why you need this

A show card is not the same for every show. Tonight Lata Ke Geet at Sur Sangam Hall has 8 of its 280 seats left, and Ira wants that card to say so in amber: "Only 8 left". The Last Show at Moti Mahal has sold all 460 seats, and its card must say "Sold out", not "0 seats left". Five showtimes in Diwali week sell out. Shows certified A, like Chai, Sutta aur Startups at The Attic, need a note before anyone books, so that nobody pays for a family outing and is turned away at the door: Datta does not want those refunds in Diwali week. A show for everyone needs no note at all. A search that finds nothing must say so, not leave a blank box. And the Basket button in the header must not say "Basket 0".

Chapter 1 said that only an expression fits in { }, and an if is not one. So far every component returned the same shape every time, filled with different values. This chapter is about returning a different shape when the data calls for it.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    SeatsBadge.jsx: Sold out, Only N left, or N seats left

    SeatsBadge gets two props, seats (all the seats) and booked (the seats sold); the seats left are seats minus booked. Make it return a paragraph that says exactly one of three things: Sold out when no seats are left; Only 7 left when 1 to 20 seats are left (with the number, here 7); 27 seats left when more than 20 are left (with the number, here 27). Exactly 20 left is still Only 20 left, and 21 left is 21 seats left. The Check tries 0, 1, 7, 20, 21 and 98 seats left.

    Give the paragraph className="badge" and a second class for its case, so app.css colours it: badge sold-out, badge few or badge plenty. The Check reads only the text.

  2. Task 2

    CertificateNote.jsx: a note, or nothing at all

    CertificateNote gets a show's certificate in its certificate prop and returns a paragraph, with className="cert-note" for app.css, that says exactly: Adults only: 18 and over for "A"; Under 13 only with an adult for "UA 13+"; Under 7 only with an adult for "UA 7+". For "U", a show for everyone, it shows nothing at all: no paragraph, no empty element, nothing on the page. The Check looks at what the component puts on the page for each of the four certificates.

  3. Task 3

    Results.jsx: the matches, or what to try

    Results gets two props: query (what was searched for) and shows (the showtimes that matched, an array of objects with an id, a title, a venue and a time). Make it return a <section>, with className="results" for app.css, that holds:

    When there are shows: a heading h2 that says 3 shows match "pune" (the number of shows, then the query in double quotes), or 1 show matches "bros" when there is exactly one; then a list, a <ul>, with an item, an <li>, for each show in order, whose text is the title, the venue and the time with commas: Biryani Bros, Chitra Mandir, 6:00 pm. Key each item by the show's id: two showtimes can have the same title.

    When shows is empty: a heading h2 that says No shows match "hamlet in marathi" (with the query), then a paragraph that says Try a film, a play or an artist., and no list at all, and no stray 0 anywhere.

  4. Task 4 · interview question

    Interview: BasketButton.jsx, the basket that said 0

    BasketButton gets the number of seats in the basket in its count prop and shows a button that says Basket, followed by a small badge, a <span className="count">, with the count. With count={2} the button says Basket with a badge saying 2. With count={0} there must be no badge at all, so the button says only Basket; today it says Basket 0, as the Preview's header shows. Fix it, and do not leave an empty badge behind: with count={0} the button must hold no empty element.

    Rukhsana will ask: why does {count && ...} show a 0 on the page, and what should the left side of && be?

What you will learn

  • if and an early return: choose before the JSX, in plain JavaScript.
  • ? : (the conditional operator): choose between two values inside JSX.
  • &&: show something or nothing, and why its left side must be true or false.
  • null: a component that returns it shows nothing at all.
  • Choosing a className or a label: the same tools, for an attribute or a word.
  • What React keeps: the same tag in the same place keeps its element; a different tag replaces it.

More in Thinking in components

Build "Conditional rendering" yourself

Parda sells tickets for Pune's own cinemas and stages, and its booking page broke on opening night. You rebuild it in React. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of React are free. This one comes with the all labs plan.