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
+37-18Lines changed: 37 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -317,31 +317,50 @@ Event triggers are component specific and are listed in the documentation for ea
317
317
318
318
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.
319
319
320
-
## Yield Events
320
+
## Page Load Events
321
321
322
-
For more complex event handling, Reflex supports yielding from event handlers to send multiple state updates to the frontend. This is particularly useful for:
322
+
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:
323
323
324
-
- Showing loading states during long operations
325
-
- Streaming data as it becomes available
326
-
- Chaining multiple events together
324
+
- Fetching data when a page first loads
325
+
- Checking authentication status
326
+
- Initializing page-specific state
327
+
- Setting default values for cookies or browser storage
327
328
328
-
To use yield events, simply use the Python `yield` keyword in your event handler:
329
+
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:
329
330
330
331
```python
331
-
@rx.event
332
-
asyncdefload_data(self):
333
-
self.loading =True
334
-
yield# First update: Show loading spinner
335
-
336
-
# Perform long operation
337
-
await asyncio.sleep(1)
338
-
339
-
self.data = [dict(id=1, name="Item 1")]
340
-
self.loading =False
341
-
# Final update: Hide spinner and show data
332
+
classState(rx.State):
333
+
data: dict= {}
334
+
335
+
@rx.event
336
+
defget_data(self):
337
+
# Fetch data when the page loads
338
+
self.data = fetch_data()
339
+
340
+
@rx.page(on_load=State.get_data)
341
+
defindex():
342
+
return rx.text('Data loaded on page load')
343
+
```
344
+
345
+
This is particularly useful for authentication checks:
346
+
347
+
```python
348
+
classState(rx.State):
349
+
authenticated: bool=False
350
+
351
+
@rx.event
352
+
defcheck_auth(self):
353
+
# Check if user is authenticated
354
+
self.authenticated = check_auth()
355
+
ifnotself.authenticated:
356
+
return rx.redirect('/login')
357
+
358
+
@rx.page(on_load=State.check_auth)
359
+
defprotected_page():
360
+
return rx.text('Protected content')
342
361
```
343
362
344
-
For more details on yield events, see the [yield events documentation](/docs/events/yield_events).
363
+
For more details on page load events, see the [page load events documentation](/docs/events/page_load_events).
0 commit comments