Sub topic 3
Showing Pending State with React Router Framework Mode
Assume you know React components, TypeScript, HTTP status codes, forms, and cookies. In framework mode, route modules coordinate navigation, server data, mutations, and rendering.
Pending UI with `useNavigation`
useNavigation reports the router's current transition. Use it to disable a submit button or show progress while a loader or action runs.
import { Form, useNavigation } from "react-router";
export default function NewContact() {
const navigation = useNavigation();
const saving = navigation.state === "submitting";
return (
<Form method="post">
<label>
Name
<input name="name" required />
</label>
<button disabled={saving}>
{saving ? "Saving..." : "Save"}
</button>
</Form>
);
}The states are idle, loading, and submitting. Do not use a local boolean that can become out of sync with the router.
Error Boundaries and 404 Responses
Export ErrorBoundary from a route module to render failures near their source. A loader can throw a response for a missing record:
import { data, isRouteErrorResponse } from "react-router";
export async function loader({ params }) {
const contact = await findContact(params.contactId);
if (!contact) throw data("Contact not found", { status: 404 });
return { contact };
}
export function ErrorBoundary({ error }) {
if (isRouteErrorResponse(error)) {
return <h1>{error.status}: {error.statusText}</h1>;
}
return <h1>Unexpected error</h1>;
}Fetchers and Optimistic UI
useFetcher submits or loads data without changing the URL. An optimistic UI updates immediately, then reconciles with the server. Use it only when failure can be handled safely.
Metadata and Stylesheets
Route modules can export meta for titles and descriptions, and links for stylesheets. Keep page-specific metadata with the route that owns the page.
Sessions and Protected Routes
Cookie sessions store signed session data. A protected loader reads the session and redirects unauthenticated users before returning private data. Keep session secrets in environment variables and server-only modules.
Streaming with `Await`
For slow, nonessential data, return a promise with defer-style streaming support and render it through Suspense and Await. This lets the main page appear before every query finishes.
Client Loaders and Hydration
clientLoader loads or combines browser-only data. HydrateFallback displays temporary markup while server-rendered content becomes interactive. Avoid accessing browser APIs in server loaders.
Resource Routes
A resource route returns a response instead of a page. Use it for JSON APIs, downloads, or generated files. Validate authorization and input just as you would for a page action.
Choosing a Rendering Strategy
| Strategy | Best fit |
|---|---|
| SSR | Dynamic pages and personalized data |
| Prerendering | Public pages known at build time |
| SPA mode | Client-only applications with no server rendering |
Building and Deploying
Run the production build command defined by the project. Deploy the generated server and client assets together, configure SESSION_SECRET, and use a production cookie setting with secure: true.
Exercise
When should a contact form use useFetcher instead of <Form>?
Show answer
Use useFetcher when the operation should submit without navigating or changing the URL.