Skip to content

Routing

The optional router uses the browser History API. It is separate from React core.

from pyreact_forge import component, html
from pyreact_forge.router import Link, Router

@component
def Home():
    return html.h1("Home")

@component
def User(user_id):
    return html.h1(f"User {user_id}")

@component
def App():
    return html.main(
        html.nav(Link("Home", href="/"), Link("User", href="/users/42")),
        Router(routes={"/": Home, "/users/:user_id": User}),
    )

Patterns support literal paths, named :parameters and a trailing * wildcard. Parameter names must be distinct Python identifiers. Query/hash are preserved in use_location() but ignored while matching paths.

navigate(path, replace=False) changes location programmatically. Internal links preserve ordinary modified-click behavior; external links should remain anchors.

Production hosting

Configure the host to serve index.html for application routes. A browser reload at /users/42 must not become a static-file 404. The local preview server already has this fallback.

The current application exporter assumes origin-root hosting. Publishing this app under /some-repository/ is not supported in the alpha. The documentation site itself uses Zensical and supports a GitHub Pages project subpath.

This router does not implement nested layouts, data loaders or route-level code splitting. Use the capability matrix to assess the project's scope.