Reflex Cloud - Fast, secure & scalable hosting. One command to deploy.

Pages

Pages in Reflex allow you to define components for different URLs. 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 calling app.add_page with the same arguments.

Links are accessible elements used primarily for navigation. Use the href 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.

Check out the docs here to learn more.

Redirect

Redirect the user to a new path within the application using rx.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 to False.

Redirect can also be run from an event handler in State, meaning logic can be added behind it. It is necessary to return the rx.redirect().

https://github.com/reflex-dev/reflex/

Nested Routes

Pages can also have nested routes.

This component will be available at /nested/page.

The router.page.path attribute allows you to obtain the path of the current page from the router data, for dynamic 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, use router.page.raw_path. This will contain all query parameters and dynamic path segments.

In the above example, current_page_route will contain the route pattern (e.g., /posts/[id]), while current_page_url will contain the actual URL (e.g., http://example.com/posts/123).

To get the full URL, access the same attributes with full_ prefix.

Example:

In this example, running on localhost should display http://localhost:3000/user/hey/posts/3/

Getting Client IP

You can use the router.session.client_ip attribute to obtain the IP address of the client associated with the current state.