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

state: _init_event_handlers recursively #1640

Merged
merged 2 commits into from
Aug 25, 2023
Merged

Conversation

masenf
Copy link
Collaborator

@masenf masenf commented Aug 21, 2023

Allow event handlers defined in parent states to be directly called from substates.

Previously, only event handlers defined on the substate could be called on the substate. A workaround for queueing event handlers defined in a parent state was to explicitly return/yield the event handler/spec (return State.handlerA())

import reflex as rx


class State(rx.State):
    def handlerA(self):
        print("handlerA")


class Substate(State):
    def handlerB(self):
        self.handlerA()  # does NOT work before this change
        print("handlerB")
        self.handlerC()
        return State.handlerA()  # does work

    def handlerC(self):
        print("handlerC")


def index() -> rx.Component:
    return rx.vstack(
        rx.button("click", on_click=Substate.handlerB),
        padding_top="10%",
    )


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

Before this change, the output is

handlerB
handlerC
handlerA

After this change, the output is

handlerA
handlerB
handlerC
handlerA

Reported in discord by community member Samsam

masenf added 2 commits August 20, 2023 21:20
Allow event handlers defined in parent states to be directly called from
substates.
@masenf masenf added this to the v0.2.6 milestone Aug 21, 2023
Copy link
Contributor

@picklelo picklelo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

@picklelo picklelo merged commit 12e516d into main Aug 25, 2023
@masenf masenf deleted the masenf/call-parent-handlers branch August 29, 2023 22:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants