Reflex Conditional Rendering: Python if, rx.cond, or rx.match?
A decision guide for Reflex conditional rendering: choose Python if, rx.cond, or rx.match based on reactivity, with a status-panel example and verification steps.
01 Sept 2026, 15:47 UTC

The Problem: A Python if on a State Var Never Updates
You are building a Reflex app and need to show different text depending on a status field in State. You write something like if State.status == "loading": inside your component body. The page renders the initial status correctly, but when an event handler changes State.status, the UI does not change. The component appears frozen even though the backend state has moved.
Useful Takeaway
Reflex compiles Python components to a React/Next.js frontend. A component function executes once on the server during the initial render. After that, UI updates happen only when a reactive Var changes and the frontend re-evaluates the conditional. Plain Python if is not reactive, so it must be reserved for values fixed at render time. Use rx.cond for two branches and rx.match for three or more branches driven by State.
Decision Table: Pick the Construct by Reactivity
| Construct | Reactivity | When to Use | Cost |
|---|---|---|---|
Python if / else | None; evaluated once at render | Constants, props, environment flags, or @rx.var values fixed at render | Cheapest |
rx.cond | Reactive; re-evaluates when referenced Vars change | Binary branch on a State Var (loading flag, boolean toggle) | Both branches constructed |
rx.match | Reactive multi-branch; re-evaluates when referenced Vars change | Three or more branches on a State Var (status, role, step) | Each branch constructed |
Trade-Offs
- Python if: Runs once. Putting a State Var in the condition fails silently: it captures the initial value and never updates, which can pass a smoke test and still ship stale UI.
- rx.cond: Reconstructs both branch component objects on the server when the condition changes. Keep branches lightweight or move expensive subtrees into separate components to avoid wasted work.
- rx.match: Cleaner than nested
rx.condchains for three or more values. Every branch must be a component, and conditions must be Var expressions the frontend can evaluate.
Concrete Example: Status Panel with rx.match
This example tracks a job status in State and renders a different panel for each value. The status starts as "idle", moves to "running", then settles on "done" or "error".
import reflex as rx
class State(rx.State):
status: str = "idle"
message: str = ""
async def run_job(self):
self.status = "running"
self.message = "Working..."
await rx.sleep(1)
self.status = "done"
self.message = "Job finished"
def status_panel():
return rx.match(
State.status,
("idle", rx.button("Run Job", on_click=State.run_job)),
("running", rx.vstack(rx.spinner(), rx.text(State.message))),
("done", rx.text(State.message, color="green")),
("error", rx.text(State.message, color="red")),
)
def index():
return rx.vstack(status_panel())
When run_job changes State.status, the frontend re-evaluates rx.match and swaps the button for a spinner, then for the done message. No full page reload occurs.
Verification Checklist
- Create the app: In a terminal, run
reflex initin an empty project directory. Replace the generated Python file with the State and component code above. No special permissions are needed beyond running thereflexCLI installed withpip install reflex. - Record the version: Run
pip show reflexand note the version. Reflex is a fast-moving 0.x project, so pin the version you develop against and check the changelog before upgrading. - Run the app: Execute
reflex runfrom the project directory. Open the local URL shown in the terminal. - Observe reactivity: Click Run Job. The button should disappear, the spinner should appear, and then the done message should replace it without a browser reload.
- Confirm the pitfall: Temporarily replace
rx.matchwith a plainif State.status == "running":block and rerun. The UI will show the initial state and will not flip when the button is clicked. - Inspect network traffic: Open browser developer tools, open the Network tab, and click the button. Look for a state-update event payload to confirm the backend change reached the frontend.
Limitations
- Component names, event-handler signatures, and state APIs can change between Reflex 0.x releases. Pin your dependency and verify
rx.condandrx.matchexist in your installed version before relying on them. - Both branches of
rx.condand every branch ofrx.matchare constructed on the server. Avoid expensive computation or side effects in untaken branches; extract them into their own components. - A Python
ifon a Var renders once and never updates. It is safe only for render-time constants.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.