Reflex Cloud - Fast, secure & scalable hosting. One command to deploy.

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.

0

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 returning an event will send the event to the frontend and unblock the queue.

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.

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.