Hello everyone, I’m just starting to use relfex and probably understand something wrong in the event handling.
What i want to achieve is basically having a list of clickable items (e.g. buttons) that behave different depending on which button was clicked. But since the list may contain a lot of elements i dont want to write one event handler per button.
I tried to solve it myself by going through examples but couldn’t make it work so here is my minimal example:
import reflex as rx
class State(rx.State):
data = ["a", "b", "c", "d"]
def draw_button(name: str) -> rx.Component:
return rx.button(name, on_click=rx.toast(name))
def index() -> rx.Component:
return rx.foreach(
State.data,
draw_button
)
app = rx.App()
app.add_page(index)
What i expected to happen was to see 4 buttons with different label (a,b,c,d), that on click cause a different toast (a,b,c,d).
Instead when running the app I get:
“reflex.utils.exceptions.VarTypeError: Cannot convert Var ‘(name === “”)’ to bool for use with
if
,and
,or
, andnot
. Instead userx.cond
and bitwise operators&
(and),|
(or),~
(invert).”
When testing it with a constant string in the rx.toast() function it worked fine.
Thanks in advance for any help.