Text 1
class CondState(rx.State):
show: bool = True
def change(self):
self.show = not (self.show)
rx.vstack(
rx.button("Toggle", on_click=CondState.change),
rx.cond(
CondState.show,
rx.text("Text 1", color="blue"),
rx.text("Text 2", color="red"),
),
)
~
to negate a condition.Text 1
Text 2
class CondState(rx.State):
show: bool = True
def change(self):
self.show = not (self.show)
rx.vstack(
rx.button("Toggle", on_click=CondState.change),
rx.cond(
CondState.show,
rx.text("Text 1", color="blue"),
rx.text("Text 2", color="red"),
),
rx.cond(
~CondState.show,
rx.text("Text 1", color="blue"),
rx.text("Text 2", color="red"),
),
)
&
and |
to make up complex conditionsTrue & True => True
True & False => False
True | False => True
class MultiCondState(rx.State):
cond1: bool = True
cond2: bool = False
cond3: bool = True
def change(self):
self.cond1 = not (self.cond1)
rx.vstack(
rx.button("Toggle", on_click=MultiCondState.change),
rx.text(
rx.cond(MultiCondState.cond1, "True", "False"),
" & True => ",
rx.cond(
MultiCondState.cond1 & MultiCondState.cond3,
"True",
"False",
),
),
rx.text(
rx.cond(MultiCondState.cond1, "True", "False"),
" & False => ",
rx.cond(
MultiCondState.cond1 & MultiCondState.cond2,
"True",
"False",
),
),
rx.text(
rx.cond(MultiCondState.cond1, "True", "False"),
" | False => ",
rx.cond(
MultiCondState.cond1 | MultiCondState.cond2,
"True",
"False",
),
),
)
Render one of two components based on a condition.