--- 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 (