Migrating from Remix and React Router
These tables map the form and mutation features of Remix and React Router to Hamur. They cover forms and handlers only; the tables on pages, data and the server in Migrating from Nuxt apply to any app. none means Hamur has no equivalent today.
The shape is one thing in Hamur: a handler, a typed function that runs on the server, exported from server/index.ts. A form posts to it (Form action={fn}), a component calls it (useAction(fn), useAsyncData(fn, input)), and with a route a URL answers it. It works without JavaScript, it is a public endpoint that must check who calls it, and it needs no directive.
The route types (+types, href) and Vite's import.meta.env and modes are in Types and environment.
| Remix, React Router | Hamur | Differences |
|---|---|---|
export async function action({ request }) in a route | a handler exported from server/index.ts | the page posts to it with <Form action={fn}> |
await request.formData() | input of the handler | decoded by formToObject |
return data({ errors }, { status: 400 }) | return fail(400, { errors }) | |
redirect("/x") | redirect("/x") | |
<Form method="post" action reloadDocument replace> | <Form action={fn} reloadDocument replace> | reset, instance, refresh and restore are added; Form posts through a handler or navigates with GET |
useActionData() | useAction(fn).state, .failure, .error | readable anywhere in the app, not only in the route |
useNavigation() | useNavigation() | state is idle or loading, plus revalidation; the fields of a running submission are in pendingRuns(app) |
useFetcher() with fetcher.Form, submit, load, state, data, formData | useAction(fn, { instance }) and <Form action={fn} instance={key}> | a keyed run that does not navigate; useForm(fn).for(key) reads one |
useFetchers() | pendingRuns(app) from hamur/adapter | |
useSubmit() | useAction(fn).run(input) | a JSON call, not a form post |
useRevalidator() | useRouter().refresh() and useNavigation().revalidation | |
shouldRevalidate({ defaultShouldRevalidate }) | export const shouldRefresh in the page | defaultShouldRefresh for defaultShouldRevalidate, plus currentUrl and nextUrl |
optimistic UI from fetcher.formData | useOptimistic(value, reduce) | pending lists the running calls, each with its formData |
a hidden intent field and a switch in action | one handler per intent | |
clientAction, clientLoader | none | handlers run on the server; browser-only data is useAsyncData(key, fn, { server: false }) |
unstable_parseMultipartFormData | form: { onFile } | |
resource routes (a route with a loader and no component) | defineHandler({ route, raw: true }) | the loader becomes the handler, and it returns a Response or a value. See A resource route |
loader and useLoaderData | useAsyncData(fn, input), or x.load.ts and useData() | load for a status decided before render |
useFormAction | none | Form resolves its own action |
A resource route
A route module that exports a loader and no component answers a URL with data or a file. In Hamur it is a handler with a route and raw: true. The request is event.request, the route params are read with getRouterParam(event, name), and a returned Response is sent as it is.
import { defineHandler, getRouterParam } from "hamur/server";
export const reportCsv = defineHandler({
route: "/reports/:id/export",
method: "GET",
raw: true,
handler(_, { event }) {
const id = getRouterParam(event, "id") as string;
return new Response(`id,total\n${id},42\n`, { headers: { "content-type": "text/csv; charset=utf-8" } });
},
});
The handler also answers POST or any other method when you set method, and method: "*" answers all of them. The route rules of the URL, such as headers or swr, apply to it like to any other path; see Route rules.