R

ReflexKR

Reflex Docs

Reflex Basics, Hands-On

·

Course overview

1

Click and it changes: State and events

2

Now you see it: conditional rendering

3

Drawing lists: rx.foreach

4

Why doesn't if work? Compile time vs. runtime

5

Making it look good: components, props and style

6

Apps, pages and a fruit basket

Reflex Basics, Hands-On
Lesson 2

Time 0:08–0:13

Now you see it: conditional rendering

"Welcome" if you're logged in, "Please log in" if you're not. Apps often need to show different content depending on their state. In this lesson, you'll use rx.cond to change the page based on a State value.

2-1. Switching content with rx.cond (3 min)

class LoginState(rx.State):
    logged_in: bool = False

    @rx.event
    def toggle_login(self):
        self.logged_in = not self.logged_in


def show_login():
    return rx.box(
        rx.cond(
            LoginState.logged_in,
            rx.heading("Logged In", as_="h2"),
            rx.heading("Not Logged In", as_="h2"),
        ),
        rx.button("Toggle Login", on_click=LoginState.toggle_login),
    )

localhost:3000

Try it

Not Logged In

Every click on Toggle Login switches the heading.

rx.cond takes three arguments.

PositionArgumentIn this example
1The condition (a Var)LoginState.logged_in
2The component to show when the condition is trueThe "Logged In" heading
3The component to show when the condition is falseThe "Not Logged In" heading
  • The third argument is optional. If you write rx.cond(LoginState.logged_in, rx.text("Welcome!")), the text appears only when you're logged in, and nothing is shown otherwise.
  • The toggle_login handler is an ordinary event handler, just like in Lesson 1. It uses not to flip between true and false.

2-2. So why not just use if? (2 min)

In plain Python, you'd be tempted to write this:

def show_login():
    if LoginState.logged_in:
        return rx.heading("Logged In", as_="h2")
    return rx.heading("Not Logged In", as_="h2")

This code doesn't work. A UI function runs only once, when the app starts, and at that point there's no way to know whether the user is logged in. rx.cond plants an instruction in the page instead: "check the value later, in the browser, and pick one."

Lesson 4 explains in detail why this happens. For now, just remember: "to branch on a State value in UI code, use rx.cond."

Exercises

  1. In show_login, make the button label change with the state too, switching between "Log In" and "Log Out". Hint: put an rx.cond(...) where the button's child goes.
  2. rx.cond works for prop values as well. Make the heading's color "green" when logged in and "gray" otherwise.

Checkpoint

  • You can explain what each of the three arguments to rx.cond is.
  • You know what happens when you leave out the third argument.
  • You know not to use if on State values in UI code.

← Previous

Lesson 1 · Click and it changes: State and events

Next →

Lesson 3 · Drawing lists: rx.foreach

Get news about new lessons

We'll email you when this course gets new lessons or a follow-up course opens.

We collect: your email address · Purpose: news about new lessons or the launch of this course · Kept until: you ask us to remove it. We never use it for anything else or share it. Reply to any notice email and we'll delete it right away.

Questions & answers

Ask anything about this lesson. Questions from every language version of this page appear together.

Shown as plain text, without links or formatting.

R

ReflexKR

Learn Python and Reflex by doing. Every example was run and verified on Reflex 0.10.0.

ReflexKR is free, with no banner ads. If it helped you, you can support it with a coffee. ☕ Buy us a coffee

Affiliate disclosure
한국어English日本語简体中文EspañolDeutsch

© 2026 ReflexKR · Since 2026 · CC BY-NC 4.0

Built with Reflex