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/pages/dynamic_routing.md
+25-60Lines changed: 25 additions & 60 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,6 @@ import reflex as rx
3
3
from pcweb import constants, styles
4
4
```
5
5
6
-
7
6
# Dynamic Routes
8
7
9
8
Dynamic routes in Reflex allow you to handle varying URL structures, enabling you to create flexible
@@ -12,17 +11,17 @@ and optional catch-all routes, each with detailed examples.
12
11
13
12
## Regular Dynamic Routes
14
13
15
-
Regular dynamic routes in Reflex allow you to match specific segments in a URL dynamically. A regular dynamic route is defined by square brackets in a route string / url pattern. For example `/users/[id]` or `/products/[category]`. These dynamic route arguments can be accessed through a state var. For the examples above they would be `rx.State.id` and `rx.State.category` respectively.
14
+
Regular dynamic routes in Reflex allow you to match specific segments in a URL dynamically. A regular dynamic route is defined by square brackets in a route string / url pattern. For example `/users/[id]` or `/products/[category]`. These dynamic route arguments can be accessed through a state var. For the examples above they would be `rx.State.id` and `rx.State.category` respectively.
16
15
17
16
```md alert info
18
17
# Why is the state var accessed as `rx.State.id`?
18
+
19
19
The dynamic route arguments are accessible as `rx.State.id` and `rx.State.category` here as the var is added to the root state, so that it is accessible from any state.
20
20
```
21
21
22
22
Example:
23
23
24
24
```python
25
-
26
25
@rx.page(route='/post/[pid]')
27
26
defpost():
28
27
'''A page that updates based on the route.'''
@@ -36,12 +35,10 @@ The [pid] part in the route is a dynamic segment, meaning it can match any value
36
35
37
36
If a user navigates to `/post/5`, `State.post_id` will return `5`, and the page will display `5` as the heading. If the URL is `/post/xyz`, it will display `xyz`. If the URL is `/post/` without any additional parameter, it will display `""`.
38
37
39
-
40
38
### Adding Dynamic Routes
41
39
42
40
Adding dynamic routes uses the `add_page` method like any other page. The only difference is that the route string contains dynamic segments enclosed in square brackets.
43
41
44
-
45
42
If you are using the `app.add_page` method to define pages, it is necessary to add the dynamic routes first, especially if they use the same function as a non dynamic route.
46
43
47
44
For example the code snippet below will:
@@ -55,12 +52,11 @@ app.add_page(index)
55
52
But if we switch the order of adding the pages, like in the example below, it will not work:
0 commit comments