--- title: action new: true --- # `action` Route actions are the "writes" to route [loader][loader] "reads". They provide a way for apps to perform data mutations with simple HTML and HTTP semantics while React Router abstracts away the complexity of asynchronous UI and revalidation. This gives you the simple mental model of HTML + HTTP (where the browser handles the asynchrony and revalidation) with the behavior and UX capabilities of modern SPAs. This feature only works if using a data router, see [Picking a Router][pickingarouter] ```tsx } action={async ({ params, request }) => { let formData = await request.formData(); return fakeUpdateSong(params.songId, formData); }} loader={({ params }) => { return fakeGetSong(params.songId); }} /> ``` Actions are called whenever the app sends a non-get submission ("post", "put", "patch", "delete") to your route. This can happen in a few ways: ```tsx // forms
; ; // imperative submissions let submit = useSubmit(); submit(data, { method: "delete", action: "/songs/123", }); fetcher.submit(data, { method: "patch", action: "/songs/123/edit", }); ``` ## `params` Route params are parsed from [dynamic segments][dynamicsegments] and passed to your action. This is useful for figuring out which resource to mutate: ```tsx { return fakeDeleteProject(params.projectId); }} /> ``` ## `request` This is a [Fetch Request][request] instance being sent to your route. The most common use case is to parse the [FormData][formdata] from the request ```tsx { let formData = await request.formData(); // ... }} /> ``` > A Request?! It might seem odd at first that actions receive a "request". Have you ever written this line of code? ```tsx [3] { event.preventDefault(); // ... }} /> ``` What exactly are you preventing? Without JavaScript, just plain HTML and an HTTP web server, that default event that was prevented is actually pretty great. Browsers will serialize all the data in the form into [`FormData`][formdata] and send it as the body of a new request to your server. Like the code above, React Router [``][form] prevents the browser from sending that request and instead sends the request to your route action! This enables highly dynamic web apps with the simple model of HTML and HTTP. Remember that the values in the `formData` are automatically serialized from the form submission, so your inputs need a `name`. ```tsx