Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF-3146] call_script with callback is broken when used as an event trigger #3520

Closed
masenf opened this issue Jun 19, 2024 · 0 comments · Fixed by #3521
Closed

[REF-3146] call_script with callback is broken when used as an event trigger #3520

masenf opened this issue Jun 19, 2024 · 0 comments · Fixed by #3521
Labels
bug Something isn't working
Milestone

Comments

@masenf
Copy link
Collaborator

masenf commented Jun 19, 2024

Describe the bug
Should be able to define a callback that works for an event trigger as well as a yielded event.

To Reproduce

import reflex as rx


class State(rx.State):
    def backend_window_location(self):
        return rx.call_script("window.location", callback=rx.console_log)


def index() -> rx.Component:
    return rx.button(
        "Click me (trigger)",
        on_click=rx.call_script("window.location", callback=rx.console_log),
    ), rx.button(
        "Click me (backend)",
        on_click=State.backend_window_location,
    )


app = rx.App()
app.add_page(index)

Expected behavior
Both buttons should work, instead the (trigger) button logs ReferenceError: queueEvents is not defined

Screenshots
Generated code looks like

export function Button_9e6a11fb76a7c9135d5bd38a5497820e () {
  const [addEvents, connectErrors] = useContext(EventLoopContext);

  const on_click_0896422d679c8ba7a632ce2c70df7485 = useCallback((_e) => addEvents([Event("_call_script", {javascript_code:`window.location`,callback:(_result) => {queueEvents([Event("_console", {message:_result})], socket); processEvent(socket)}})], (_e), {}), [addEvents, Event])


  return (
    <RadixThemesButton onClick={on_click_0896422d679c8ba7a632ce2c70df7485}>
  {`Click me`}
</RadixThemesButton>
  )
}

Specifics (please complete the following information):

  • Python Version: 3.11
  • Reflex Version: 0.5.1+
  • OS:
  • Browser (Optional):

Additional context
Introduced via #3216

REF-3146

@masenf masenf added the bug Something isn't working label Jun 19, 2024
@masenf masenf changed the title call_script with callback is broken when used as an event trigger [REF-3146] call_script with callback is broken when used as an event trigger Jun 19, 2024
@masenf masenf modified the milestones: Open Source Bugs, v.39 Jun 19, 2024
masenf added a commit that referenced this issue Jun 19, 2024
The handler for _call_script always calls `eval` on the `callback` string
anyway, and this allows the callback to be specified for backend-originated
call_script as well as frontend (triggered) call_script.

Fix #3520
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant