dataResponse: dataResponse (loader/action helper)

View source: R/helpers.R

dataResponseR Documentation

dataResponse (loader/action helper)

Description

https://api.reactrouter.com/v7/functions/react-router.data.html

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.

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 });
  }")

Value

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

Examples

## Not run: 
Route(
  path = "/profile",
  loader = dataResponse(
    list(name = "Ada", role = "Engineer"),
    init = list(status = 200)
  ),
  element = useLoaderData(tags$pre())
)

## End(Not run)


reactRouter documentation built on May 10, 2026, 5:07 p.m.