React Router Adapter
Use oRPC inside a React Router project by mounting a handler in a resource route.
React Router is a multi-strategy router for React, and the successor to Remix. In framework mode, its resource routes follow the Fetch API, so oRPC integrates through the Fetch API Adapter.
Basic
import type { ActionFunctionArgs, LoaderFunctionArgs } from 'react-router'
import { onError } from '@orpc/server'
import { RPCHandler } from '@orpc/server/fetch'
const handler = new RPCHandler(router, {
interceptors: [
onError((error) => {
console.error(error)
}),
],
})
async function handleRequest(request: Request) {
const { response } = await handler.handle(request, {
prefix: '/rpc',
context: {} // Provide initial context if needed
})
return response ?? new Response('Not found', { status: 404 })
}
export async function loader({ request }: LoaderFunctionArgs) {
return handleRequest(request)
}
export async function action({ request }: ActionFunctionArgs) {
return handleRequest(request)
}import type { RouteConfig } from '@react-router/dev/routes'
import { index, route } from '@react-router/dev/routes'
export default [
index('routes/home.tsx'),
route('rpc/*', 'routes/rpc.ts'),
] satisfies RouteConfig