Skip to content

How to run arbitrary javascript in response to an event? #1996

Answered by masenf
masenf asked this question in Q&A
Discussion options

You must be logged in to vote

rx.client_side was just syntax sugar over arbitrary javascript inside a Var with type EventChain. These Var objects may be created directly with more control over the results.

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, rx.call_script must be used.


The cust…

Replies: 3 comments 1 reply

Comment options

masenf
Oct 18, 2023
Maintainer Author

You must be logged in to vote
0 replies
Answer selected by masenf
Comment options

You must be logged in to vote
1 reply
@masenf
Comment options

masenf Oct 26, 2023
Maintainer Author

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants