V Knowledgebase

Interactive code demo

React Router action response

Submit the form to see a local browser demonstration of reading form data, creating an item, and returning a response body with a custom header and status code.

Create an item

Try the action flow

This form creates a real record from your input and saves it in this browser using local storage, so it remains available after a refresh. It does not send the data to a server or create an actual React Router HTTP response.

Loader demo

Load a product by route parameter

Choose a product ID to preview how a React Router loader reads params.pid, fetches a product, and passes it to the page component.

React Router example

Loader and product component

In a configured React Router app, params.pid comes from the route segment (for example, products/:pid). Replace getProduct with your real data source.

import { Route } from "./+types/product";

export async function loader({ params }: Route.LoaderArgs) {
  const product = await getProduct(params.pid);
  return { product };
}

export default function Product({ loaderData }: Route.ComponentProps) {
  return <div>{loaderData.product.name}</div>;
}

Note: this static-site preview uses sample products in your browser. It demonstrates the loader-to-component data flow but does not run React Router or load data from a server.

React Router configuration

Enable the Vite Environment API

Place this in react-router.config.ts in a configured React Router project. It enables the v8 Vite Environment API future flag; it does not run in this static-site preview by itself.

import type { Config } from "@react-router/dev/config";

export default {
  future: {
    v8_viteEnvironmentApi: true,
  },
} satisfies Config;

Corrected example

Server-side React Router action

For a real HTTP response with status 201 and a custom header, run this in a configured React Router application with a real createItem implementation. The comma after headers is required.

import { data } from "react-router";

export async function action({ request }: Route.ActionArgs) {
  const formData = await request.formData();
  const item = await createItem(formData);

  return data(item, {
    headers: { "X-Custom-Header": "value" },
    status: 201,
  });
}

Important: this static-site demo mirrors the form-to-result flow only. It cannot run a React Router server action or create an actual HTTP 201 response.