% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/helpers.R
\name{reactRouterHelpers}
\alias{reactRouterHelpers}
\title{JavaScript helpers exposed on \code{window.jsmodule['@/reactRouter'].helpers}}
\description{
For convenience, the package exposes a curated set of React Router v7
utility functions under \code{window.jsmodule['@/reactRouter'].helpers}
so user-authored \code{\link{JS}} loaders, actions, and render callbacks
can call them without reaching into webpack internals. The same names are
available as on the JS side of React Router itself.
}
\details{
Scoped under the \pkg{shiny.react} \code{jsmodule} registry rather than
\code{window} so the package never adds a top-level global to the host
page. A short alias is the only ergonomic cost:
\preformatted{
  loader = JS("async () => {
    const { redirect } = window.jsmodule['@/reactRouter'].helpers;
    if (!await ok()) return redirect('/login');
    return { ok: true };
  }")
}

\strong{Loader/action response helpers}
\itemize{
  \item \code{redirect(to, init?)} -- client-side navigation redirect.
  \item \code{replace(to, init?)} -- redirect that replaces the history entry.
  \item \code{redirectDocument(to)} -- full document reload redirect.
  \item \code{data(value, init?)} -- attach status/headers to a payload.
}

\strong{Path / URL utilities}
\itemize{
  \item \code{generatePath(path, params)} -- build a URL from a pattern.
  \item \code{matchPath(pattern, pathname)} -- match a pathname.
  \item \code{matchRoutes(routes, location, basename?)} -- match a route
    tree against a location.
  \item \code{resolvePath(to, fromPathname?)} -- resolve a relative path.
  \item \code{parsePath(path)} -- split into \code{pathname/search/hash}.
  \item \code{createPath(parts)} -- inverse of \code{parsePath}.
  \item \code{createSearchParams(init)} -- build a \code{URLSearchParams}.
}

These run in the browser only (inside \code{\link{JS}} loader/action code).
For R-side construction of a \code{Link()} \code{to} value from data, use
base R: \code{paste0("/users/", utils::URLencode(id, reserved = TRUE))}.

\strong{Error helpers}
\itemize{
  \item \code{isRouteErrorResponse(error)} -- type guard intended for use
    inside an \code{errorElement} alongside \code{\link{useRouteError}};
    returns \code{true} when the error came from a thrown
    \code{Response} (e.g. \code{throw new Response(..., { status: 404 })}).
}

These are the JavaScript implementations from \code{react-router-dom}.
}
\examples{
\dontrun{
# Conditional redirect inside a custom loader.
Route(
  path = "/admin",
  loader = JS("async () => {
    const { redirect } = window.jsmodule['@/reactRouter'].helpers;
    const ok = await checkAuth();
    if (!ok) return redirect('/login');
    return { ok: true };
  }"),
  element = useLoaderData(tags$pre())
)

# Branch on whether the route error is a Response.
Route(
  path = "/items/:id",
  loader = JS("async ({ params }) => {
    const r = await fetch('/api/items/' + params.id);
    if (!r.ok) throw new Response('Not found', { status: 404 });
    return r.json();
  }"),
  errorElement = useRouteError(
    render = JS(
      "e => window.jsmodule['@/reactRouter'].helpers.isRouteErrorResponse(e)" +
      " ? <p>HTTP {e.status}</p> : <p>Unknown error</p>"
    )
  ),
  element = useLoaderData(tags$pre())
)
}

}
\keyword{internal}
