reflex icon indicating copy to clipboard operation
reflex copied to clipboard

Event chaining in the on_load event handler return is not working.

Open heumsi opened this issue 1 year ago β€’ 4 comments

Describe the bug A clear and concise description of what the bug is.

Event chaining in the on_load event handler return is not working.

To Reproduce Steps to reproduce the behavior:

  • Code/Link to Repo:

I implemented it like this

class BaseState(pc.State):
    ...


class QueryState(BaseState):
    label: str

    def init(self):
        print("init!")
        return [
            self.reset_label,
        ]

    def reset_label(self):
        print("reset label!")
        self.label = ""

app = pc.App(state=state.BaseState)
app.add_page(query.index, route="/", on_load=QueryState.init)

When I ran the code, the result was as follows.

init!

The reset_label() method doesn't seem to have actually been executed.

Expected behavior A clear and concise description of what you expected to happen.

The reset_label() method should be executed. This means that the result of its execution should be

init!
reset label!

Screenshots If applicable, add screenshots to help explain your problem.

** Specifics (please complete the following information):**

  • Python Version: 3.11.0
  • Pynecone Version: 0.1.18
  • OS: Mac OS Monterey 12.6
  • Browser (Optional): .

Additional context Add any other context about the problem here.

heumsi avatar Feb 27 '23 16:02 heumsi

Currently event chains don't work in on_load since on_load events are handled in HydrateMiddleware.

A different method for handling on_load events has been proposed to handle chaining, but is not yet implemented (see #603).

As a work around in the meantime, you can use a list of event handlers in on_load:

    ...


class QueryState(BaseState):
    label: str

    def init(self):
        print("init!")

    def reset_label(self):
        print("reset label!")
        self.label = ""

app = pc.App(state=state.BaseState)
app.add_page(query.index, route="/", 
                        on_load=[QueryState.init, 
                                 QueryState.reset_label]
                       )

lucashofer avatar Feb 27 '23 16:02 lucashofer

I modified it like this

app.add_page(query.index, route="/", on_load=[QueryState.init, QueryState.reset_label])

However, we still get the following error.

AttributeError: 'list' object has no attribute 'fn'

As a work around in the meantime, you can use a list of event handlers in on_load:

I'm using the most recent version, 0.1.18, but the list of event handlers for on_load doesn't seem to be implemented yet.

heumsi avatar Mar 01 '23 07:03 heumsi

Okay. I'll consider it a bug and wait for it to be fixed. Thanks for all your hard work.

heumsi avatar Mar 01 '23 07:03 heumsi

The bug has been fixed since 0.1.18.

If you clone the repo and then pip install it, it should work

lucashofer avatar Mar 01 '23 10:03 lucashofer