Skip to contents

Usage

dataResponse(value, init = NULL)

Arguments

value

The payload to expose via useLoaderData() / useActionData(). Either an R object (list, vector, data.frame – serialized to JSON), or a JS expression for a JavaScript value.

init

Optional. Either a list with status (integer), statusText (character) and/or headers (named list), or a JS expression evaluating to such an object.

Value

A JS expression suitable for the loader or action argument of Route.

Details

Returns a JS loader function that resolves to a React Router data() response – a thin wrapper that lets you attach an HTTP status, statusText, and/or headers alongside the loader/action payload while still exposing value via useLoaderData / useActionData.

Use the R helper for static loaders that always return the same value plus status. For values computed inside a custom loader/action, call window.jsmodule['@/reactRouter'].helpers.data(value, init) directly in your JS() string, e.g.


  loader = JS("async () => {
    const { data } = window.jsmodule['@/reactRouter'].helpers;
    const rows = await fetchRows();
    return data({ rows }, { status: 200 });
  }")

Examples

if (FALSE) { # \dontrun{
Route(
  path = "/profile",
  loader = dataResponse(
    list(name = "Ada", role = "Engineer"),
    init = list(status = 200)
  ),
  element = useLoaderData(tags$pre())
)
} # }