# React Router > Thank you for helping improve our documentation! --- --- title: Await --- # Await [MODES: framework, data] ## Summary [Reference Documentation ↗](https://api.reactrouter.com/v7/functions/react_router.Await.html) Used to render promise values with automatic error handling. **Note:** `` expects to be rendered inside a [``](https://react.dev/reference/react/Suspense) ```tsx import { Await, useLoaderData } from "react-router"; export async function loader() { // not awaited const reviews = getReviews(); // awaited (blocks the transition) const book = await fetch("/api/book").then((res) => res.json()); return { book, reviews }; } function Book() { const { book, reviews } = useLoaderData(); return (

{book.title}

{book.description}

}> Could not load reviews 😬
} children={(resolvedReviews) => ( )} />
); } ``` ## Signature ```tsx function Await({ children, errorElement, resolve, }: AwaitProps) ``` ## Props ### children When using a function, the resolved value is provided as the parameter. ```tsx [2] {(resolvedReviews) => } ``` When using React elements, [`useAsyncValue`](../hooks/useAsyncValue) will provide the resolved value: ```tsx [2] function Reviews() { const resolvedReviews = useAsyncValue(); return
...
; } ``` ### errorElement The error element renders instead of the `children` when the [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) rejects. ```tsx Oops} resolve={reviewsPromise} > ``` To provide a more contextual error, you can use the [`useAsyncError`](../hooks/useAsyncError) in a child component ```tsx } resolve={reviewsPromise} > function ReviewsError() { const error = useAsyncError(); return
Error loading reviews: {error.message}
; } ``` If you do not provide an `errorElement`, the rejected value will bubble up to the nearest route-level [`ErrorBoundary`](../../start/framework/route-module#errorboundary) and be accessible via the [`useRouteError`](../hooks/useRouteError) hook. ### resolve Takes a [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) returned from a [`loader`](../../start/framework/route-module#loader) to be resolved and rendered. ```tsx import { Await, useLoaderData } from "react-router"; export async function loader() { let reviews = getReviews(); // not awaited let book = await getBook(); return { book, reviews, // this is a promise }; } export default function Book() { const { book, reviews, // this is the same promise } = useLoaderData(); return (

{book.title}

{book.description}

}>
); } ``` --- --- title: Form --- # Form [MODES: framework, data] ## Summary [Reference Documentation ↗](https://api.reactrouter.com/v7/functions/react_router.Form.html) A progressively enhanced HTML [`
`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form) that submits data to actions via [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/fetch), activating pending states in [`useNavigation`](../hooks/useNavigation) which enables advanced user interfaces beyond a basic HTML [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form). After a form's `action` completes, all data on the page is automatically revalidated to keep the UI in sync with the data. Because it uses the HTML form API, server rendered pages are interactive at a basic level before JavaScript loads. Instead of React Router managing the submission, the browser manages the submission as well as the pending states (like the spinning favicon). After JavaScript loads, React Router takes over enabling web application user experiences. `Form` is most useful for submissions that should also change the URL or otherwise add an entry to the browser history stack. For forms that shouldn't manipulate the browser [`History`](https://developer.mozilla.org/en-US/docs/Web/API/History) stack, use [``](https://api.reactrouter.com/v7/types/react_router.FetcherWithComponents.html#Form). ```tsx import { Form } from "react-router"; function NewEvent() { return ( ); } ``` ## Props ### action The URL to submit the form data to. If `undefined`, this defaults to the closest route in context. ### discover Defines the form [lazy route discovery](../../explanation/lazy-route-discovery) behavior. - **render** — default, discover the route when the form renders - **none** — don't eagerly discover, only discover if the form is submitted ```tsx
// default ("render") ``` ### encType The encoding type to use for the form submission. ```tsx // Default ``` ### fetcherKey Indicates a specific fetcherKey to use when using `navigate={false}` so you can pick up the fetcher's state in a different component in a [`useFetcher`](../hooks/useFetcher). ### method The HTTP verb to use when the form is submitted. Supports `"delete"`, `"get"`, `"patch"`, `"post"`, and `"put"`. Native [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form) only supports `"get"` and `"post"`, avoid the other verbs if you'd like to support progressive enhancement ### navigate When `false`, skips the navigation and submits via a fetcher internally. This is essentially a shorthand for [`useFetcher`](../hooks/useFetcher) + `` where you don't care about the resulting data in this component. ### onSubmit A function to call when the form is submitted. If you call [`event.preventDefault()`](https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault) then this form will not do anything. ### preventScrollReset Prevent the scroll position from resetting to the top of the viewport on completion of the navigation when using the ```` component ### relative Determines whether the form action is relative to the route hierarchy or the pathname. Use this if you want to opt out of navigating the route hierarchy and want to instead route based on slash-delimited URL segments. See [`RelativeRoutingType`](https://api.reactrouter.com/v7/types/react_router.RelativeRoutingType.html). ### reloadDocument Forces a full document navigation instead of client side routing and data fetch. ### replace Replaces the current entry in the browser [`History`](https://developer.mozilla.org/en-US/docs/Web/API/History) stack when the form navigates. Use this if you don't want the user to be able to click "back" to the page with the form on it. ### state State object to add to the [`History`](https://developer.mozilla.org/en-US/docs/Web/API/History) stack entry for this navigation ### viewTransition Enables a [View Transition](https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API) for this navigation. To apply specific styles during the transition, see [`useViewTransitionState`](../hooks/useViewTransitionState). ### unstable_defaultShouldRevalidate Specify the default revalidation behavior after this submission If no `shouldRevalidate` functions are present on the active routes, then this value will be used directly. Otherwise it will be passed into `shouldRevalidate` so the route can make the final determination on revalidation. This can be useful when updating search params and you don't want to trigger a revalidation. By default (when not specified), loaders will revalidate according to the routers standard revalidation behavior. --- --- title: Link --- # Link [MODES: framework, data, declarative] ## Summary [Reference Documentation ↗](https://api.reactrouter.com/v7/functions/react_router.Link.html) A progressively enhanced [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a) wrapper to enable navigation with client-side routing. ```tsx import { Link } from "react-router"; Dashboard; ; ``` ## Props ### discover [modes: framework] Defines the link [lazy route discovery](../../explanation/lazy-route-discovery) behavior. - **render** — default, discover the route when the link renders - **none** — don't eagerly discover, only discover if the link is clicked ```tsx // default ("render") ``` ### prefetch [modes: framework] Defines the data and module prefetching behavior for the link. ```tsx // default ``` - **none** — default, no prefetching - **intent** — prefetches when the user hovers or focuses the link - **render** — prefetches when the link renders - **viewport** — prefetches when the link is in the viewport, very useful for mobile Prefetching is done with HTML [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link) tags. They are inserted after the link. ```tsx // might conditionally render ``` Because of this, if you are using `nav :last-child` you will need to use `nav :last-of-type` so the styles don't conditionally fall off your last link (and any other similar selectors). ### preventScrollReset [modes: framework, data] Prevents the scroll position from being reset to the top of the window when the link is clicked and the app is using [`ScrollRestoration`](../components/ScrollRestoration). This only prevents new locations resetting scroll to the top, scroll position will be restored for back/forward button navigation. ```tsx ``` ### relative [modes: framework, data, declarative] Defines the relative path behavior for the link. ```tsx // default: "route" ``` Consider a route hierarchy where a parent route pattern is `"blog"` and a child route pattern is `"blog/:slug/edit"`. - **route** — default, resolves the link relative to the route pattern. In the example above, a relative link of `"..."` will remove both `:slug/edit` segments back to `"/blog"`. - **path** — relative to the path so `"..."` will only remove one URL segment up to `"/blog/:slug"` Note that index routes and layout routes do not have paths so they are not included in the relative path calculation. ### reloadDocument [modes: framework, data, declarative] Will use document navigation instead of client side routing when the link is clicked: the browser will handle the transition normally (as if it were an [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a)). ```tsx ``` ### replace [modes: framework, data, declarative] Replaces the current entry in the [`History`](https://developer.mozilla.org/en-US/docs/Web/API/History) stack instead of pushing a new one onto it. ```tsx ``` ``` # with a history stack like this A -> B # normal link click pushes a new entry A -> B -> C # but with `replace`, B is replaced by C A -> C ``` ### state [modes: framework, data, declarative] Adds persistent client side routing state to the next location. ```tsx ``` The location state is accessed from the `location`. ```tsx function SomeComp() { const location = useLocation(); location.state; // { some: "value" } } ``` This state is inaccessible on the server as it is implemented on top of [`history.state`](https://developer.mozilla.org/en-US/docs/Web/API/History/state) ### to [modes: framework, data, declarative] Can be a string or a partial [`Path`](https://api.reactrouter.com/v7/interfaces/react_router.Path.html): ```tsx ``` ### viewTransition [modes: framework, data] Enables a [View Transition](https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API) for this navigation. ```jsx Click me ``` To apply specific styles for the transition, see [`useViewTransitionState`](../hooks/useViewTransitionState) ### unstable_defaultShouldRevalidate [modes: framework, data, declarative] Specify the default revalidation behavior for the navigation. ```tsx ``` If no `shouldRevalidate` functions are present on the active routes, then this value will be used directly. Otherwise it will be passed into `shouldRevalidate` so the route can make the final determination on revalidation. This can be useful when updating search params and you don't want to trigger a revalidation. By default (when not specified), loaders will revalidate according to the routers standard revalidation behavior. --- --- title: Links --- # Links [MODES: framework] ## Summary [Reference Documentation ↗](https://api.reactrouter.com/v7/functions/react_router.Links.html) Renders all the [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link) tags created by the route module's [`links`](../../start/framework/route-module#links) export. You should render it inside the [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head) of your document. ```tsx import { Links } from "react-router"; export default function Root() { return ( ); } ``` ## Signature ```tsx function Links({ nonce }: LinksProps): React.JSX.Element ``` ## Props ### nonce A [`nonce`](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/nonce) attribute to render on the [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link) element --- --- title: Meta --- # Meta [MODES: framework] ## Summary [Reference Documentation ↗](https://api.reactrouter.com/v7/functions/react_router.Meta.html) Renders all the [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta) tags created by the route module's [`meta`](../../start/framework/route-module#meta) export. You should render it inside the [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head) of your document. ```tsx import { Meta } from "react-router"; export default function Root() { return ( ); } ``` ## Signature ```tsx function Meta(): React.JSX.Element ``` --- --- title: NavLink --- # NavLink [MODES: framework, data, declarative] ## Summary [Reference Documentation ↗](https://api.reactrouter.com/v7/functions/react_router.NavLink.html) Wraps [``](../components/Link) with additional props for styling active and pending states. - Automatically applies classes to the link based on its `active` and `pending` states, see [`NavLinkProps.className`](https://api.reactrouter.com/v7/interfaces/react_router.NavLinkProps.html#className) - Note that `pending` is only available with Framework and Data modes. - Automatically applies `aria-current="page"` to the link when the link is active. See [`aria-current`](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-current) on MDN. - States are additionally available through the className, style, and children render props. See [`NavLinkRenderProps`](https://api.reactrouter.com/v7/types/react_router.NavLinkRenderProps.html). ```tsx Messages // Using render props isPending ? "pending" : isActive ? "active" : "" } > Messages ``` ## Props ### caseSensitive [modes: framework, data, declarative] Changes the matching logic to make it case-sensitive: | Link | URL | isActive | | -------------------------------------------- | ------------- | -------- | | `` | `/sponge-bob` | true | | `` | `/sponge-bob` | false | ### children [modes: framework, data, declarative] Can be regular React children or a function that receives an object with the `active` and `pending` states of the link. ```tsx {({ isActive }) => ( Tasks )} ``` ### className [modes: framework, data, declarative] Classes are automatically applied to `NavLink` that correspond to the state. ```css a.active { color: red; } a.pending { color: blue; } a.transitioning { view-transition-name: my-transition; } ``` Or you can specify a function that receives [`NavLinkRenderProps`](https://api.reactrouter.com/v7/types/react_router.NavLinkRenderProps.html) and returns the `className`: ```tsx ( isActive ? "my-active-class" : isPending ? "my-pending-class" : "" )} /> ``` ### discover [modes: framework] Defines the link [lazy route discovery](../../explanation/lazy-route-discovery) behavior. - **render** — default, discover the route when the link renders - **none** — don't eagerly discover, only discover if the link is clicked ```tsx // default ("render") ``` ### end [modes: framework, data, declarative] Changes the matching logic for the `active` and `pending` states to only match to the "end" of the [`NavLinkProps.to`](https://api.reactrouter.com/v7/interfaces/react_router.NavLinkProps.html#to). If the URL is longer, it will no longer be considered active. | Link | URL | isActive | | ----------------------------- | ------------ | -------- | | `` | `/tasks` | true | | `` | `/tasks/123` | true | | `` | `/tasks` | true | | `` | `/tasks/123` | false | `` is an exceptional case because _every_ URL matches `/`. To avoid this matching every single route by default, it effectively ignores the `end` prop and only matches when you're at the root route. ### prefetch [modes: framework] Defines the data and module prefetching behavior for the link. ```tsx // default ``` - **none** — default, no prefetching - **intent** — prefetches when the user hovers or focuses the link - **render** — prefetches when the link renders - **viewport** — prefetches when the link is in the viewport, very useful for mobile Prefetching is done with HTML [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link) tags. They are inserted after the link. ```tsx // might conditionally render ``` Because of this, if you are using `nav :last-child` you will need to use `nav :last-of-type` so the styles don't conditionally fall off your last link (and any other similar selectors). ### preventScrollReset [modes: framework, data] Prevents the scroll position from being reset to the top of the window when the link is clicked and the app is using [`ScrollRestoration`](../components/ScrollRestoration). This only prevents new locations resetting scroll to the top, scroll position will be restored for back/forward button navigation. ```tsx ``` ### relative [modes: framework, data, declarative] Defines the relative path behavior for the link. ```tsx // default: "route" ``` Consider a route hierarchy where a parent route pattern is `"blog"` and a child route pattern is `"blog/:slug/edit"`. - **route** — default, resolves the link relative to the route pattern. In the example above, a relative link of `"..."` will remove both `:slug/edit` segments back to `"/blog"`. - **path** — relative to the path so `"..."` will only remove one URL segment up to `"/blog/:slug"` Note that index routes and layout routes do not have paths so they are not included in the relative path calculation. ### reloadDocument [modes: framework, data, declarative] Will use document navigation instead of client side routing when the link is clicked: the browser will handle the transition normally (as if it were an [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a)). ```tsx ``` ### replace [modes: framework, data, declarative] Replaces the current entry in the [`History`](https://developer.mozilla.org/en-US/docs/Web/API/History) stack instead of pushing a new one onto it. ```tsx ``` ``` # with a history stack like this A -> B # normal link click pushes a new entry A -> B -> C # but with `replace`, B is replaced by C A -> C ``` ### state [modes: framework, data, declarative] Adds persistent client side routing state to the next location. ```tsx ``` The location state is accessed from the `location`. ```tsx function SomeComp() { const location = useLocation(); location.state; // { some: "value" } } ``` This state is inaccessible on the server as it is implemented on top of [`history.state`](https://developer.mozilla.org/en-US/docs/Web/API/History/state) ### style [modes: framework, data, declarative] Styles can also be applied dynamically via a function that receives [`NavLinkRenderProps`](https://api.reactrouter.com/v7/types/react_router.NavLinkRenderProps.html) and returns the styles: ```tsx ({ color: isActive ? "red" : isPending ? "blue" : "black" })} /> ``` ### to [modes: framework, data, declarative] Can be a string or a partial [`Path`](https://api.reactrouter.com/v7/interfaces/react_router.Path.html): ```tsx ``` ### viewTransition [modes: framework, data] Enables a [View Transition](https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API) for this navigation. ```jsx Click me ``` To apply specific styles for the transition, see [`useViewTransitionState`](../hooks/useViewTransitionState) --- --- title: Navigate --- # Navigate [MODES: framework, data, declarative] ## Summary [Reference Documentation ↗](https://api.reactrouter.com/v7/functions/react_router.Navigate.html) A component-based version of [`useNavigate`](../hooks/useNavigate) to use in a [`React.Component` class](https://react.dev/reference/react/Component) where hooks cannot be used. It's recommended to avoid using this component in favor of [`useNavigate`](../hooks/useNavigate). ```tsx ``` ## Signature ```tsx function Navigate({ to, replace, state, relative }: NavigateProps): null ``` ## Props ### relative How to interpret relative routing in the `to` prop. See [`RelativeRoutingType`](https://api.reactrouter.com/v7/types/react_router.RelativeRoutingType.html). ### replace Whether to replace the current entry in the [`History`](https://developer.mozilla.org/en-US/docs/Web/API/History) stack ### state State to pass to the new [`Location`](https://api.reactrouter.com/v7/interfaces/react_router.Location.html) to store in [`history.state`](https://developer.mozilla.org/en-US/docs/Web/API/History/state). ### to The path to navigate to. This can be a string or a [`Path`](https://api.reactrouter.com/v7/interfaces/react_router.Path.html) object --- --- title: Outlet --- # Outlet [MODES: framework, data, declarative] ## Summary [Reference Documentation ↗](https://api.reactrouter.com/v7/functions/react_router.Outlet.html) Renders the matching child route of a parent route or nothing if no child route matches. ```tsx import { Outlet } from "react-router"; export default function SomeParent() { return (

Parent Content

); } ``` ## Signature ```tsx function Outlet(props: OutletProps): React.ReactElement | null ``` ## Props ### context Provides a context value to the element tree below the outlet. Use when the parent route needs to provide values to child routes. ```tsx ``` Access the context with [`useOutletContext`](../hooks/useOutletContext). --- --- title: PrefetchPageLinks --- # PrefetchPageLinks [MODES: framework] ## Summary [Reference Documentation ↗](https://api.reactrouter.com/v7/functions/react_router.PrefetchPageLinks.html) Renders [``](https://developer.mozilla.org/en-US/docs/Web/API/HTMLLinkElement/rel) tags for modules and data of another page to enable an instant navigation to that page. [``](./Link#prefetch) uses this internally, but you can render it to prefetch a page for any other reason. For example, you may render one of this as the user types into a search field to prefetch search results before they click through to their selection. ```tsx import { PrefetchPageLinks } from "react-router"; ``` ## Signature ```tsx function PrefetchPageLinks({ page, ...linkProps }: PageLinkDescriptor) ``` ## Props ### page The absolute path of the page to prefetch, e.g. `/absolute/path`. ### linkProps Additional props to spread onto the [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link) tags, such as [`crossOrigin`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLLinkElement/crossOrigin), [`integrity`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLLinkElement/integrity), [`rel`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLLinkElement/rel), etc. --- --- title: Route --- # Route [MODES: framework, data, declarative] ## Summary [Reference Documentation ↗](https://api.reactrouter.com/v7/functions/react_router.Route.html) Configures an element to render when a pattern matches the current location. It must be rendered within a [`Routes`](../components/Routes) element. Note that these routes do not participate in data loading, actions, code splitting, or any other route module features. ```tsx // Usually used in a declarative router function App() { return ( } /> } /> } /> ); } // But can be used with a data router as well if you prefer the JSX notation const routes = createRoutesFromElements( <> ); const router = createBrowserRouter(routes); function App() { return ; } ``` ## Signature ```tsx function Route(props: RouteProps): React.ReactElement | null ``` ## Props ### action The route action. See [`action`](../../start/data/route-object#action). ### caseSensitive Whether the path should be case-sensitive. Defaults to `false`. ### Component The React Component to render when this route matches. Mutually exclusive with `element`. ### children Child Route components ### element The React element to render when this Route matches. Mutually exclusive with `Component`. ### ErrorBoundary The React Component to render at this route if an error occurs. Mutually exclusive with `errorElement`. ### errorElement The React element to render at this route if an error occurs. Mutually exclusive with `ErrorBoundary`. ### handle The route handle. ### HydrateFallback The React Component to render while this router is loading data. Mutually exclusive with `hydrateFallbackElement`. ### hydrateFallbackElement The React element to render while this router is loading data. Mutually exclusive with `HydrateFallback`. ### id The unique identifier for this route (for use with [`DataRouter`](https://api.reactrouter.com/v7/interfaces/react_router.DataRouter.html)s) ### index Whether this is an index route. ### lazy A function that returns a promise that resolves to the route object. Used for code-splitting routes. See [`lazy`](../../start/data/route-object#lazy). ### loader The route loader. See [`loader`](../../start/data/route-object#loader). ### path The path pattern to match. If unspecified or empty, then this becomes a layout route. ### shouldRevalidate The route shouldRevalidate function. See [`shouldRevalidate`](../../start/data/route-object#shouldRevalidate). --- --- title: Routes --- # Routes [MODES: framework, data, declarative] ## Summary [Reference Documentation ↗](https://api.reactrouter.com/v7/functions/react_router.Routes.html) Renders a branch of [``s](../components/Route) that best matches the current location. Note that these routes do not participate in [data loading](../../start/framework/route-module#loader), [`action`](../../start/framework/route-module#action), code splitting, or any other [route module](../../start/framework/route-module) features. ```tsx import { Route, Routes } from "react-router"; } /> } /> } /> ``` ## Signature ```tsx function Routes({ children, location, }: RoutesProps): React.ReactElement | null ``` ## Props ### children Nested [`Route`](../components/Route) elements ### location The [`Location`](https://api.reactrouter.com/v7/interfaces/react_router.Location.html) to match against. Defaults to the current location. --- --- title: Scripts --- # Scripts [MODES: framework] ## Summary [Reference Documentation ↗](https://api.reactrouter.com/v7/functions/react_router.Scripts.html) Renders the client runtime of your app. It should be rendered inside the [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/body) of the document. If server rendering, you can omit `` and the app will work as a traditional web app without JavaScript, relying solely on HTML and browser behaviors. ```tsx import { Scripts } from "react-router"; export default function Root() { return ( ); } ``` ## Signature ```tsx function Scripts(scriptProps: ScriptsProps): React.JSX.Element | null ``` ## Props ### scriptProps Additional props to spread onto the [` ``` --- [$link]: https://www.youtube.com/watch?v=dQw4w9WgXcQ [createbrowserrouter]: ./routers/create-browser-router --- --- title: Backend For Frontend --- # Backend For Frontend [MODES: framework]

While React Router can serve as your fullstack application, it also fits perfectly into the "Backend for Frontend" architecture. The BFF strategy employs a web server with a job scoped to serving the frontend web app and connecting it to the services it needs: your database, mailer, job queues, existing backend APIs (REST, GraphQL), etc. Instead of your UI integrating directly from the browser to these services, it connects to the BFF, and the BFF connects to your services. Mature apps already have a lot of backend application code in Ruby, Elixir, PHP, etc., and there's no reason to justify migrating it all to a server-side JavaScript runtime just to get the benefits of React Router. Instead, you can use your React Router app as a backend for your frontend. You can use `fetch` right from your loaders and actions to your backend. ```tsx lines=[7,13,17] import escapeHtml from "escape-html"; export async function loader() { const apiUrl = "https://api.example.com/some-data.json"; const res = await fetch(apiUrl, { headers: { Authorization: `Bearer ${process.env.API_TOKEN}`, }, }); const data = await res.json(); const prunedData = data.map((record) => { return { id: record.id, title: record.title, formattedBody: escapeHtml(record.content), }; }); return { prunedData }; } ``` There are several benefits of this approach vs. fetching directly from the browser. The highlighted lines above show how you can: 1. Simplify third-party integrations and keep tokens and secrets out of client bundles 2. Prune the data down to send less kB over the network, speeding up your app significantly 3. Move a lot of code from browser bundles to the server, like `escapeHtml`, which speeds up your app. Additionally, moving code to the server usually makes your code easier to maintain since server-side code doesn't have to worry about UI states for async operations Again, React Router can be used as your only server by talking directly to the database and other services with server-side JavaScript APIs, but it also works perfectly as a backend for your frontend. Go ahead and keep your existing API server for application logic and let React Router connect the UI to it. --- --- title: Automatic Code Splitting --- # Automatic Code Splitting [MODES: framework]

When using React Router's framework features, your application is automatically code split to improve the performance of initial load times when users visit your application. ## Code Splitting by Route Consider this simple route config: ```tsx filename=app/routes.ts import { type RouteConfig, route, } from "@react-router/dev/routes"; export default [ route("/contact", "./contact.tsx"), route("/about", "./about.tsx"), ] satisfies RouteConfig; ``` Instead of bundling all routes into a single giant build, the modules referenced (`contact.tsx` and `about.tsx`) become entry points to the bundler. Because these entry points are coupled to URL segments, React Router knows just from a URL which bundles are needed in the browser, and more importantly, which are not. If the user visits `"/about"` then the bundles for `about.tsx` will be loaded but not `contact.tsx`. This drastically reduces the JavaScript footprint for initial page loads and speeds up your application. ## Removal of Server Code Any server-only [Route Module APIs][route-module] will be removed from the bundles. Consider this route module: ```tsx export async function loader() { return { message: "hello" }; } export async function action() { console.log(Date.now()); return { ok: true }; } export async function headers() { return { "Cache-Control": "max-age=300" }; } export default function Component({ loaderData }) { return
{loaderData.message}
; } ``` After building for the browser, only the `Component` will still be in the bundle, so you can use server-only code in the other module exports. [route-module]: ../../start/framework/route-module --- --- title: Network Concurrency Management --- # Network Concurrency Management [MODES: framework, data]

When building web applications, managing network requests can be a daunting task. The challenges of ensuring up-to-date data and handling simultaneous requests often lead to complex logic in the application to deal with interruptions and race conditions. React Router simplifies this process by automating network management while mirroring and expanding upon the intuitive behavior of web browsers. To help understand how React Router handles concurrency, it's important to remember that after `form` submissions, React Router will fetch fresh data from the `loader`s. This is called revalidation. ## Natural Alignment with Browser Behavior React Router's handling of network concurrency is heavily inspired by the default behavior of web browsers when processing documents. ### Link Navigation **Browser Behavior**: When you click on a link in a browser and then click on another before the page transition completes, the browser prioritizes the most recent `action`. It cancels the initial request, focusing solely on the latest link clicked. **React Router Behavior**: React Router manages client-side navigation the same way. When a link is clicked within a React Router application, it initiates fetch requests for each `loader` tied to the target URL. If another navigation interrupts the initial navigation, React Router cancels the previous fetch requests, ensuring that only the latest requests proceed. ### Form Submission **Browser Behavior**: If you initiate a form submission in a browser and then quickly submit another form again, the browser disregards the first submission, processing only the latest one. **React Router Behavior**: React Router mimics this behavior when working with forms. If a form is submitted and another submission occurs before the first completes, React Router cancels the original fetch requests. It then waits for the latest submission to complete before triggering page revalidation again. ## Concurrent Submissions and Revalidation While standard browsers are limited to one request at a time for navigations and form submissions, React Router elevates this behavior. Unlike navigation, with [`useFetcher`][use_fetcher] multiple requests can be in flight simultaneously. React Router is designed to handle multiple form submissions to server `action`s and concurrent revalidation requests efficiently. It ensures that as soon as new data is available, the state is updated promptly. However, React Router also safeguards against potential pitfalls by refraining from committing stale data when other `action`s introduce race conditions. For instance, if three form submissions are in progress, and one completes, React Router updates the UI with that data immediately without waiting for the other two so that the UI remains responsive and dynamic. As the remaining submissions finalize, React Router continues to update the UI, ensuring that the most recent data is displayed. Using this key: - `|`: Submission begins - ✓: Action complete, data revalidation begins - ✅: Revalidated data is committed to the UI - ❌: Request cancelled We can visualize this scenario in the following diagram: ```text submission 1: |----✓-----✅ submission 2: |-----✓-----✅ submission 3: |-----✓-----✅ ``` However, if a subsequent submission's revalidation completes before an earlier one, React Router discards the earlier data, ensuring that only the most up-to-date information is reflected in the UI: ```text submission 1: |----✓---------❌ submission 2: |-----✓-----✅ submission 3: |-----✓-----✅ ``` Because the revalidation from submission (2) started later and landed earlier than submission (1), the requests from submission (1) are canceled and only the data from submission (2) is committed to the UI. It was requested later, so it's more likely to contain the updated values from both (1) and (2). ## Potential for Stale Data It's unlikely your users will ever experience this, but there are still chances for the user to see stale data in very rare conditions with inconsistent infrastructure. Even though React Router cancels requests for stale data, they will still end up making it to the server. Canceling a request in the browser simply releases browser resources for that request; it can't "catch up" and stop it from getting to the server. In extremely rare conditions, a canceled request may change data after the interrupting `action`'s revalidation lands. Consider this diagram: ```text 👇 interruption with new submission |----❌----------------------✓ |-------✓-----✅ 👆 initial request reaches the server after the interrupting submission has completed revalidation ``` The user is now looking at different data than what is on the server. Note that this problem is both extremely rare and exists with default browser behavior, too. The chance of the initial request reaching the server later than both the submission and revalidation of the second is unexpected on any network and server infrastructure. If this is a concern with your infrastructure, you can send timestamps with your form submissions and write server logic to ignore stale submissions. ## Example In UI components like comboboxes, each keystroke can trigger a network request. Managing such rapid, consecutive requests can be tricky, especially when ensuring that the displayed results match the most recent query. However, with React Router, this challenge is automatically handled, ensuring that users see the correct results without developers having to micromanage the network. ```tsx filename=app/pages/city-search.tsx export async function loader({ request }) { const { searchParams } = new URL(request.url); const cities = await searchCities(searchParams.get("q")); return cities; } export function CitySearchCombobox() { const fetcher = useFetcher(); return ( // submit the form onChange to get the list of cities fetcher.submit(event.target.form) } /> {/* render with the loader's data */} {fetcher.data ? ( {fetcher.data.length > 0 ? ( {fetcher.data.map((city) => ( ))} ) : ( No results found )} ) : null} ); } ``` All the application needs to know is how to query the data and how to render it. React Router handles the network. ## Conclusion React Router offers developers an intuitive, browser-based approach to managing network requests. By mirroring browser behaviors and enhancing them where needed, it simplifies the complexities of concurrency, revalidation, and potential race conditions. Whether you're building a simple webpage or a sophisticated web application, React Router ensures that your user interactions are smooth, reliable, and always up to date. [use_fetcher]: ../api/hooks/useFetcher --- --- title: Form vs. fetcher --- # Form vs. fetcher [MODES: framework, data] ## Overview Developing in React Router offers a rich set of tools that can sometimes overlap in functionality, creating a sense of ambiguity for newcomers. The key to effective development in React Router is understanding the nuances and appropriate use cases for each tool. This document seeks to provide clarity on when and why to use specific APIs. ## APIs in Focus - [``][form-component] - [`useFetcher`][use-fetcher] - [`useNavigation`][use-navigation] Understanding the distinctions and intersections of these APIs is vital for efficient and effective React Router development. ## URL Considerations The primary criterion when choosing among these tools is whether you want the URL to change or not: - **URL Change Desired**: When navigating or transitioning between pages, or after certain actions like creating or deleting records. This ensures that the user's browser history accurately reflects their journey through your application. - **Expected Behavior**: In many cases, when users hit the back button, they should be taken to the previous page. Other times the history entry may be replaced but the URL change is important nonetheless. - **No URL Change Desired**: For actions that don't significantly change the context or primary content of the current view. This might include updating individual fields or minor data manipulations that don't warrant a new URL or page reload. This also applies to loading data with fetchers for things like popovers, combo boxes, etc. ### When the URL Should Change These actions typically reflect significant changes to the user's context or state: - **Creating a New Record**: After creating a new record, it's common to redirect users to a page dedicated to that new record, where they can view or further modify it. - **Deleting a Record**: If a user is on a page dedicated to a specific record and decides to delete it, the logical next step is to redirect them to a general page, such as a list of all records. For these cases, developers should consider using a combination of [``][form-component] and [`useNavigation`][use-navigation]. These tools can be coordinated to handle form submission, invoke specific actions, retrieve action-related data through component props, and manage navigation respectively. ### When the URL Shouldn't Change These actions are generally more subtle and don't require a context switch for the user: - **Updating a Single Field**: Maybe a user wants to change the name of an item in a list or update a specific property of a record. This action is minor and doesn't necessitate a new page or URL. - **Deleting a Record from a List**: In a list view, if a user deletes an item, they likely expect to remain on the list view, with that item no longer in the list. - **Creating a Record in a List View**: When adding a new item to a list, it often makes sense for the user to remain in that context, seeing their new item added to the list without a full page transition. - **Loading Data for a Popover or Combobox**: When loading data for a popover or combobox, the user's context remains unchanged. The data is loaded in the background and displayed in a small, self-contained UI element. For such actions, [`useFetcher`][use-fetcher] is the go-to API. It's versatile, combining functionalities of these APIs, and is perfectly suited for tasks where the URL should remain unchanged. ## API Comparison As you can see, the two sets of APIs have a lot of similarities: | Navigation/URL API | Fetcher API | | ----------------------------- | -------------------- | | `` | `` | | `actionData` (component prop) | `fetcher.data` | | `navigation.state` | `fetcher.state` | | `navigation.formAction` | `fetcher.formAction` | | `navigation.formData` | `fetcher.formData` | ## Examples ### Creating a New Record ```tsx filename=app/pages/new-recipe.tsx lines=[16,23-24,29] import { Form, redirect, useNavigation, } from "react-router"; import type { Route } from "./+types/new-recipe"; export async function action({ request, }: Route.ActionArgs) { const formData = await request.formData(); const errors = await validateRecipeFormData(formData); if (errors) { return { errors }; } const recipe = await db.recipes.create(formData); return redirect(`/recipes/${recipe.id}`); } export function NewRecipe({ actionData, }: Route.ComponentProps) { const { errors } = actionData || {}; const navigation = useNavigation(); const isSubmitting = navigation.formAction === "/recipes/new"; return (