For AI agents: the complete documentation index is at llms.txt. Remove the trailing slash from a page URL and append .md to read its Markdown version. For the docs home, use index.md.

Chaining events

Calling Event Handlers From Event Handlers

You can call other event handlers from event handlers to keep your code modular. Just use the self.call_handler syntax to run another event handler. As always, you can yield within your function to send incremental updates to the frontend.

Start Building Now!
0

Awaiting Event Handlers

An event handler accessed through its state class is awaitable. await runs it right away, inside the current event, and gives back its return value. The awaited handler runs on the same locked state as the caller, its yields send updates to the frontend as usual, and any backend events it yields or returns are queued after the current event, as if the caller had yielded them; frontend-only events are sent to the frontend immediately.

Start Building Now!

Total: 0

Compared to calling the handler as a method of self, awaiting it converts the arguments like an event from the frontend, sends an update when it yields, and works for a handler of any state in the app, not only the caller's own state.

Returning Events From Event Handlers

So far, we have only seen events that are triggered by components. However, an event handler can also return events.

In Reflex, event handlers run synchronously, so only one event handler can run at a time, and the events in the queue will be blocked until the current event handler finishes. The difference between returning an event and calling an event handler is that a returned event is queued and runs after the current handler finishes.

Be sure to use the class name State (or any substate) rather than self when returning events.

Try entering an integer in the input below then clicking out.

Start Building Now!
1

In this example, we run the Collatz Conjecture on a number entered by the user.

When the on_blur event is triggered, the event handler start_collatz is called. It sets the initial count, then calls run_step which runs until the count reaches 1.