-
Now that
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
import reflex as rx
custom_handler = rx.vars.BaseVar(
_var_name="ev => {window.alert(`You clicked at ${ev.clientX}, ${ev.clientY}`)}",
_var_type=rx.EventChain,
)
def index():
return rx.button(
"Where Did I Click?",
on_click=custom_handler,
)
app = rx.App()
app.add_page(index)
app.compile() Note: Custom event handlers added in this fashion can NOT be yielded from another event handler on the backend. For executing custom script from backend, The custom JS code may interact with the backend state by formatting custom event chains. Here is the same example, but with updating state instead of displaying a browser alert. import reflex as rx
class State(rx.State):
coordinates: tuple[int, int] = (0, 0)
def update_coordinates(self, clientX, clientY):
self.coordinates = (clientX, clientY)
custom_event_chain = rx.EventChain(
events=[State.update_coordinates("${ev.clientX}", "${ev.clientY}")]
)
custom_handler = rx.vars.BaseVar(
_var_name=f"ev => {rx.utils.format.format_event_chain(custom_event_chain)}",
_var_type=rx.EventChain,
)
def index():
return rx.vstack(
rx.heading(State.coordinates.to_string()),
rx.button(
"Where Did I Click?",
on_click=custom_handler,
)
)
app = rx.App()
app.add_page(index)
app.compile() And for posterity, here is the "proper" way to accomplish the same result without custom code: from typing import Any
import reflex as rx
class State(rx.State):
coordinates: tuple[int, int] = (0, 0)
def update_coordinates(self, clientX, clientY):
self.coordinates = (clientX, clientY)
class DOMClickEvent(rx.Base):
clientX: int
clientY: int
def click_coordinate_signature(e: DOMClickEvent):
return [e.clientX, e.clientY]
class CoordinateCaptureButton(rx.Button):
def get_event_triggers(self) -> dict[str, Any]:
return {
**super().get_event_triggers(),
rx.constants.EventTriggers.ON_CLICK: click_coordinate_signature
}
def index():
return rx.vstack(
rx.heading(State.coordinates.to_string()),
CoordinateCaptureButton.create(
"Where Did I Click?",
on_click=State.update_coordinates,
)
)
app = rx.App()
app.add_page(index)
app.compile() |
Beta Was this translation helpful? Give feedback.
-
Thanks. This is very nice. I don't see any documentation or examples on how to use |
Beta Was this translation helpful? Give feedback.
-
Any update on how to use format_event_chain on 0.6.5? I've tried str(rx.Var.create(event_chain)) that was stated to be an alternative for it, but doesn't work,The error was: TypeError: None is not a callable object. I've also changed the codes from:
To:
P.S. I just used your code as a sample, it's not my exact code but it is kinda similar. |
Beta Was this translation helpful? Give feedback.
rx.client_side
was just syntax sugar over arbitrary javascript inside aVar
with typeEventChain
. TheseVar
objects may be created directly with more control over the results.Note: Custom event handlers added in this fashion can NOT be yielded from another event handler on the backend. For executing custom script from backend,
rx.call_script
must be used.The cust…