Event Triggers

Components can modify the state based on user events such as clicking a button or entering text in a field. These events are triggered by event triggers.

Event triggers are component specific and are listed in the documentation for each component.

Component Lifecycle Events

Reflex components have lifecycle events like on_mount and on_unmount that allow you to execute code at specific points in a component's existence. These events are crucial for initializing data, cleaning up resources, and creating dynamic user interfaces.

When Lifecycle Events Are Activated

  • on_mount: This event is triggered immediately after a component is rendered and attached to the DOM. It fires:
    • When a page containing the component is first loaded
    • When a component is conditionally rendered (appears after being hidden)
    • When navigating to a page containing the component using internal navigation
    • It does NOT fire when the page is refreshed or when following external links
  • on_unmount: This event is triggered just before a component is removed from the DOM. It fires:
    • When navigating away from a page containing the component using internal navigation
    • When a component is conditionally removed from the DOM (e.g., via a condition that hides it)
    • It does NOT fire when refreshing the page, closing the browser tab, or following external links

Page Load Events

In addition to component lifecycle events, Reflex also provides page-level events like on_load that are triggered when a page loads. The on_load event is useful for:

  • Fetching data when a page first loads
  • Checking authentication status
  • Initializing page-specific state
  • Setting default values for cookies or browser storage

You can specify an event handler to run when the page loads using the on_load parameter in the @rx.page decorator or app.add_page() method:

This is particularly useful for authentication checks:

For more details on page load events, see the page load events documentation.

Event Reference

on_focus

The on_focus event handler is called when the element (or some element inside of it) receives focus. For example, it’s called when the user clicks on a text input.

on_blur

The on_blur event handler is called when focus has left the element (or left some element inside of it). For example, it’s called when the user clicks outside of a focused text input.

on_change

The on_change event handler is called when the value of an element has changed. For example, it’s called when the user types into a text input each keystroke triggers the on change.

on_click

The on_click event handler is called when the user clicks on an element. For example, it’s called when the user clicks on a button.

on_context_menu

The on_context_menu event handler is called when the user right-clicks on an element. For example, it’s called when the user right-clicks on a button.

on_double_click

The on_double_click event handler is called when the user double-clicks on an element. For example, it’s called when the user double-clicks on a button.

on_mount

The on_mount event handler is called after the component is rendered on the page. It is similar to a page on_load event, although it does not necessarily fire when navigating between pages. This event is particularly useful for initializing data, making API calls, or setting up component-specific state when a component first appears.

Component Lifecycle Demo

on_unmount

The on_unmount event handler is called after removing the component from the page. However, on_unmount will only be called for internal navigation, not when following external links or refreshing the page. This event is useful for cleaning up resources, saving state, or performing cleanup operations before a component is removed from the DOM.

Unmount Demo

Resource active

on_mouse_up

The on_mouse_up event handler is called when the user releases a mouse button on an element. For example, it’s called when the user releases the left mouse button on a button.

on_mouse_down

The on_mouse_down event handler is called when the user presses a mouse button on an element. For example, it’s called when the user presses the left mouse button on a button.

on_mouse_enter

The on_mouse_enter event handler is called when the user’s mouse enters an element. For example, it’s called when the user’s mouse enters a button.

on_mouse_leave

The on_mouse_leave event handler is called when the user’s mouse leaves an element. For example, it’s called when the user’s mouse leaves a button.

on_mouse_move

The on_mouse_move event handler is called when the user moves the mouse over an element. For example, it’s called when the user moves the mouse over a button.

on_mouse_out

The on_mouse_out event handler is called when the user’s mouse leaves an element. For example, it’s called when the user’s mouse leaves a button.

on_mouse_over

The on_mouse_over event handler is called when the user’s mouse enters an element. For example, it’s called when the user’s mouse enters a button.

on_scroll

The on_scroll event handler is called when the user scrolls the page. For example, it’s called when the user scrolls the page down.

Scroll to make the text below change.

Change Me!

Scroll to make the text above change.

Built with Reflex