Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    ⚛React

    Components and JSX

    • Why React?
    • JSX rules
    • Props
    • Rendering lists and keys
    • Conditional rendering

    State and events

    • Handling events
    • useState: a component's memory
    • Objects and arrays in state
    • Forms and controlled inputs
    • Lifting state up

    Effects and essential hooks

    • useEffect
    • Fetching data
    • useRef
    • Context: data for the whole tree
    • useReducer

    Professional React

    • Writing your own hooks
    • Performance: memo, useMemo and useCallback
    • Suspense and error boundaries
    • What's new in React 19
    • Thinking in React
    • Project: a complete to-do app
    React

    Components and JSX · Lesson 5 of 21

    Conditional rendering

    if before return, the ternary, &&, and the zero trap.

    7 minutes2 games

    Three ways to show something based on a condition:

    1. if before return, when the whole output changes (loading, error). 2. A ternary cond ? A : B inside JSX, choosing between two things. 3. cond && A to show something or nothing.

    React
    function CourseStatus({ progress, enrolled, newLessons }) {
      if (!enrolled) {
        return <p>Enroll to start learning.</p>
      }
      return (
        <div>
          <p>{progress === 100 ? "Completed ✔" : "In progress: " + progress + "%"}</p>
          {newLessons > 0 && <p>{newLessons} new lessons!</p>}
        </div>
      )
    }
    
    function App() {
      return (
        <>
          <CourseStatus enrolled={false} />
          <hr />
          <CourseStatus enrolled progress={60} newLessons={2} />
          <hr />
          <CourseStatus enrolled progress={100} newLessons={0} />
        </>
      )
    }

    Important

    Beware count && <X /> when count is a number: if it's 0, React renders a `0` on the page (because 0 && … is 0, and React renders numbers). Write count > 0 && <X />. null, undefined and false render nothing.

    React
    function App() {
      const messages = []
      return (
        <div>
          <p>Wrong: {messages.length && <b>You have mail</b>}</p>
          <p>Right: {messages.length > 0 && <b>You have mail</b>}</p>
        </div>
      )
    }

    What's the output?

    function App() {
      const items = []
      const user = null
      return (
        <p>
          {items.length && "list"}
          {user && user.name}
          {false}
          {"!"}
        </p>
      )
    }

    What will the page show?

    Interview question

    How do you handle loading, error and empty states cleanly?

    Exercise

    Finish Inbox: show No new messages for 0, 1 new message for 1, and N new messages for more. The page shows all three cases one under another.

    The page should show:
    No new messages
    1 new message
    7 new messages
    React
    function Inbox({ count }) {
      return <p>{count}</p>
    }
    
    function App() {
      return (
        <div>
          <Inbox count={0} />
          <Inbox count={1} />
          <Inbox count={7} />
        </div>
      )
    }

    Learn more from

    • react.dev: Conditional renderingreact.dev
    • MDN: Logical AND (&&)developer.mozilla.org
    • MDN: Conditional (ternary) operatordeveloper.mozilla.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousRendering lists and keysNextHandling events
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.