You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api-reference/event_triggers.md
+112-4Lines changed: 112 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,7 @@ from datetime import datetime
4
4
import reflex as rx
5
5
6
6
from pcweb.templates.docpage import docdemo, h1_comp, text_comp, docpage
7
+
from pcweb.pages.docs import events
7
8
8
9
SYNTHETIC_EVENTS= [
9
10
{
@@ -92,27 +93,70 @@ SYNTHETIC_EVENTS = [
92
93
},
93
94
{
94
95
"name": "on_mount",
95
-
"description": "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.",
96
+
"description": "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.",
"description": "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.",
133
+
"description": "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.",
@@ -270,6 +314,70 @@ These events are triggered by event triggers.
270
314
271
315
Event triggers are component specific and are listed in the documentation for each component.
272
316
317
+
## Component Lifecycle Events
318
+
319
+
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.
320
+
321
+
### When Lifecycle Events Are Activated
322
+
323
+
-**on_mount**: This event is triggered immediately after a component is rendered and attached to the DOM. It fires:
324
+
- When a page containing the component is first loaded
325
+
- When a component is conditionally rendered (appears after being hidden)
326
+
- When navigating to a page containing the component using internal navigation
327
+
- It does NOT fire when the page is refreshed or when following external links
328
+
329
+
-**on_unmount**: This event is triggered just before a component is removed from the DOM. It fires:
330
+
- When navigating away from a page containing the component using internal navigation
331
+
- When a component is conditionally removed from the DOM (e.g., via a condition that hides it)
332
+
- It does NOT fire when refreshing the page, closing the browser tab, or following external links
333
+
334
+
## Page Load Events
335
+
336
+
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:
337
+
338
+
- Fetching data when a page first loads
339
+
- Checking authentication status
340
+
- Initializing page-specific state
341
+
- Setting default values for cookies or browser storage
342
+
343
+
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:
344
+
345
+
```python
346
+
classState(rx.State):
347
+
data: dict=dict()
348
+
349
+
@rx.event
350
+
defget_data(self):
351
+
# Fetch data when the page loads
352
+
self.data = fetch_data()
353
+
354
+
@rx.page(on_load=State.get_data)
355
+
defindex():
356
+
return rx.text('Data loaded on page load')
357
+
```
358
+
359
+
This is particularly useful for authentication checks:
360
+
361
+
```python
362
+
classState(rx.State):
363
+
authenticated: bool=False
364
+
365
+
@rx.event
366
+
defcheck_auth(self):
367
+
# Check if user is authenticated
368
+
self.authenticated = check_auth()
369
+
ifnotself.authenticated:
370
+
return rx.redirect('/login')
371
+
372
+
@rx.page(on_load=State.check_auth)
373
+
defprotected_page():
374
+
return rx.text('Protected content')
375
+
```
376
+
377
+
For more details on page load events, see the [page load events documentation]({events.page_load_events.path}).
0 commit comments