Skip to content

Commit d9cea3b

Browse files
Replace yield events section with on_load events documentation
Co-Authored-By: Alek Petuskey <[email protected]>
1 parent 19a2989 commit d9cea3b

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

docs/api-reference/event_triggers.md

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -317,31 +317,50 @@ Event triggers are component specific and are listed in the documentation for ea
317317

318318
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.
319319

320-
## Yield Events
320+
## Page Load Events
321321

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:
323323

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
327328

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:
329330

330331
```python
331-
@rx.event
332-
async def load_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+
class State(rx.State):
333+
data: dict = {}
334+
335+
@rx.event
336+
def get_data(self):
337+
# Fetch data when the page loads
338+
self.data = fetch_data()
339+
340+
@rx.page(on_load=State.get_data)
341+
def index():
342+
return rx.text('Data loaded on page load')
343+
```
344+
345+
This is particularly useful for authentication checks:
346+
347+
```python
348+
class State(rx.State):
349+
authenticated: bool = False
350+
351+
@rx.event
352+
def check_auth(self):
353+
# Check if user is authenticated
354+
self.authenticated = check_auth()
355+
if not self.authenticated:
356+
return rx.redirect('/login')
357+
358+
@rx.page(on_load=State.check_auth)
359+
def protected_page():
360+
return rx.text('Protected content')
342361
```
343362

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).
345364

346365
```python eval
347366
rx.box(

0 commit comments

Comments
 (0)