Back to React Router

data

docs/api/utils/data.md

7.6.21.3 KB
Original Source

data

<!-- ⚠️ ⚠️ IMPORTANT ⚠️ ⚠️ Thank you for helping improve our documentation! This file is auto-generated from the JSDoc comments in the source code, so please edit the JSDoc comments in the file below and this file will be re-generated once those changes are merged. https://github.com/remix-run/react-router/blob/main/packages/react-router/lib/router/utils.ts -->

[MODES: framework, data]

Summary

Reference Documentation ↗

Create "responses" that contain headers/status without forcing serialization into an actual Response

tsx
import { data } from "react-router";

export async function action({ request }: Route.ActionArgs) {
  let formData = await request.formData();
  let item = await createItem(formData);
  return data(item, {
    headers: { "X-Custom-Header": "value" }
    status: 201,
  });
}

Signature

tsx
function data<D>(data: D, init?: number | ResponseInit)

Params

data

The data to be included in the response.

init

The status code or a ResponseInit object to be included in the response.

Returns

A DataWithResponseInit instance containing the data and response init.