Pages
Pages map components to different URLs in your app. This section covers creating pages, handling URL arguments, accessing query parameters, managing page metadata, and handling page load events.
Adding a Page
You can create a page by defining a function that returns a component.By default, the function name will be used as the route, but you can also specify a route.
In this example we create three pages:
index
- The root route, available at/
about
- available at/about
custom
- available at/custom-route
Index is a special exception where it is available at both/
and/index
. All other pages are only available at their specified route.
Page Decorator
You can also use the@rx.page
decorator to add a page.
This is equivalent to callingapp.add_page
with the same arguments.
Navigating Between Pages
Links
Links are accessible elements used primarily for navigation. Use thehref
prop to specify the location for the link to navigate to.
You can also provide local links to other pages in your project without writing the full url.
To open the link in a new tab, set theis_external
prop toTrue
.
Check out thelink docs to learn more.
Redirect
Redirect the user to a new path within the application usingrx.redirect()
.
path
: The destination path or URL to which the user should be redirected.external
: If set to True, the redirection will open in a new tab. Defaults toFalse
.
Redirect can also be run from an event handler in State, meaning logic can be added behind it. It is necessary toreturn
therx.redirect()
.
https://github.com/reflex-dev/reflex/
Nested Routes
Pages can also have nested routes.
This component will be available at/nested/page
.
Page Metadata
You can add page metadata such as:
- The title to be shown in the browser tab
- The description as shown in search results
- The preview image to be shown when the page is shared on social media
- Any additional metadata
Getting the Current Page
You can access the current page from therouter
attribute in any state. See therouter docs for all available attributes.
Therouter.page.path
attribute allows you to obtain the path of the current page from the router data,fordynamic pages this will contain the slug rather than the actual value used to load the page.
To get the actual URL displayed in the browser, userouter.page.raw_path
. Thiswill contain all query parameters and dynamic path segments.
In the above example,current_page_route
will contain the route pattern (e.g.,/posts/[id]
), whilecurrent_page_url
will contain the actual URL (e.g.,/posts/123
).
To get the full URL, access the same attributes withfull_
prefix.
Example:
In this example, running onlocalhost
should displayhttp://localhost:3000/posts/123/