docs/api-reference/event_triggers.md
from datetime import datetime
import reflex as rx
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.
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.
on_mount: This event is triggered immediately after a component is rendered and attached to the DOM. It fires:
on_unmount: This event is triggered just before a component is removed from the DOM. It fires:
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:
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:
class State(rx.State):
data: dict = dict()
@rx.event
def get_data(self):
# Fetch data when the page loads
self.data = fetch_data()
@rx.page(on_load=State.get_data)
def index():
return rx.text("Data loaded on page load")
This is particularly useful for authentication checks:
class State(rx.State):
authenticated: bool = False
@rx.event
def check_auth(self):
# Check if user is authenticated
self.authenticated = check_auth()
if not self.authenticated:
return rx.redirect("/login")
@rx.page(on_load=State.check_auth)
def protected_page():
return rx.text("Protected content")
For more details on page load events, see the page load events documentation.
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.
class FocusState(rx.State):
text: str = "Change Me!"
@rx.event
def change_text(self, text):
if self.text == "Change Me!":
self.text = "Changed!"
else:
self.text = "Change Me!"
def focus_example():
return rx.input(value=FocusState.text, on_focus=FocusState.change_text)
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.
class BlurState(rx.State):
text: str = "Change Me!"
@rx.event
def change_text(self, text):
if self.text == "Change Me!":
self.text = "Changed!"
else:
self.text = "Change Me!"
def blur_example():
return rx.input(value=BlurState.text, on_blur=BlurState.change_text)
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.
class ChangeState(rx.State):
checked: bool = False
@rx.event
def set_checked(self):
self.checked = not self.checked
def change_example():
return rx.switch(on_change=ChangeState.set_checked)
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.
class ClickState(rx.State):
text: str = "Change Me!"
@rx.event
def change_text(self):
if self.text == "Change Me!":
self.text = "Changed!"
else:
self.text = "Change Me!"
def click_example():
return rx.button(ClickState.text, on_click=ClickState.change_text)
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.
class ContextState(rx.State):
text: str = "Change Me!"
@rx.event
def change_text(self):
if self.text == "Change Me!":
self.text = "Changed!"
else:
self.text = "Change Me!"
def context_menu_example():
return rx.button(ContextState.text, on_context_menu=ContextState.change_text)
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.
class DoubleClickState(rx.State):
text: str = "Change Me!"
@rx.event
def change_text(self):
if self.text == "Change Me!":
self.text = "Changed!"
else:
self.text = "Change Me!"
def double_click_example():
return rx.button(
DoubleClickState.text, on_double_click=DoubleClickState.change_text
)
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.
class MountState(rx.State):
events: list[str] = []
data: list[dict] = []
loading: bool = False
@rx.event
def on_mount(self):
self.events = self.events[-4:] + ["on_mount @ " + str(datetime.now())]
@rx.event
async def load_data(self):
self.loading = True
yield
import asyncio
await asyncio.sleep(1)
self.data = [dict(id=1, name="Item 1"), dict(id=2, name="Item 2")]
self.loading = False
def mount_example():
return rx.vstack(
rx.heading("Component Lifecycle Demo"),
rx.foreach(MountState.events, rx.text),
rx.cond(
MountState.loading,
rx.spinner(),
rx.foreach(
MountState.data,
lambda item: rx.text(f"ID: {item['id']} - {item['name']}"),
),
),
on_mount=MountState.on_mount,
)
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.
class UnmountState(rx.State):
events: list[str] = []
resource_id: str = "resource-12345"
status: str = "Resource active"
@rx.event
def on_unmount(self):
self.events = self.events[-4:] + ["on_unmount @ " + str(datetime.now())]
self.status = f"Resource {self.resource_id} cleaned up"
@rx.event
def initialize_resource(self):
self.status = f"Resource {self.resource_id} initialized"
def unmount_example():
return rx.vstack(
rx.heading("Unmount Demo"),
rx.foreach(UnmountState.events, rx.text),
rx.text(UnmountState.status),
rx.link(
rx.button("Navigate Away (Triggers Unmount)"),
href="/",
),
on_mount=UnmountState.initialize_resource,
on_unmount=UnmountState.on_unmount,
)
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.
class MouseUpState(rx.State):
text: str = "Change Me!"
@rx.event
def change_text(self):
if self.text == "Change Me!":
self.text = "Changed!"
else:
self.text = "Change Me!"
def mouse_up_example():
return rx.button(MouseUpState.text, on_mouse_up=MouseUpState.change_text)
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.
class MouseDownState(rx.State):
text: str = "Change Me!"
@rx.event
def change_text(self):
if self.text == "Change Me!":
self.text = "Changed!"
else:
self.text = "Change Me!"
def mouse_down_example():
return rx.button(MouseDownState.text, on_mouse_down=MouseDownState.change_text)
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.
class MouseEnterState(rx.State):
text: str = "Change Me!"
@rx.event
def change_text(self):
if self.text == "Change Me!":
self.text = "Changed!"
else:
self.text = "Change Me!"
def mouse_enter_example():
return rx.button(MouseEnterState.text, on_mouse_enter=MouseEnterState.change_text)
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.
class MouseLeaveState(rx.State):
text: str = "Change Me!"
@rx.event
def change_text(self):
if self.text == "Change Me!":
self.text = "Changed!"
else:
self.text = "Change Me!"
def mouse_leave_example():
return rx.button(MouseLeaveState.text, on_mouse_leave=MouseLeaveState.change_text)
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.
class MouseMoveState(rx.State):
text: str = "Change Me!"
@rx.event
def change_text(self):
if self.text == "Change Me!":
self.text = "Changed!"
else:
self.text = "Change Me!"
def mouse_move_example():
return rx.button(MouseMoveState.text, on_mouse_move=MouseMoveState.change_text)
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.
class MouseOutState(rx.State):
text: str = "Change Me!"
@rx.event
def change_text(self):
if self.text == "Change Me!":
self.text = "Changed!"
else:
self.text = "Change Me!"
def mouse_out_example():
return rx.button(MouseOutState.text, on_mouse_out=MouseOutState.change_text)
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.
class MouseOverState(rx.State):
text: str = "Change Me!"
@rx.event
def change_text(self):
if self.text == "Change Me!":
self.text = "Changed!"
else:
self.text = "Change Me!"
def mouse_over_example():
return rx.button(MouseOverState.text, on_mouse_over=MouseOverState.change_text)
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.
class ScrollState(rx.State):
text: str = "Change Me!"
@rx.event
def change_text(self):
if self.text == "Change Me!":
self.text = "Changed!"
else:
self.text = "Change Me!"
def scroll_example():
return rx.vstack(
rx.text("Scroll to make the text below change."),
rx.text(ScrollState.text),
rx.text("Scroll to make the text above change."),
on_scroll=ScrollState.change_text,
overflow="auto",
height="3em",
width="100%",
)