diff --git a/src/middleware/legacy-routes-redirect.ts b/src/middleware/legacy-routes-redirect.ts index fdf371f9b..647d1b38b 100644 --- a/src/middleware/legacy-routes-redirect.ts +++ b/src/middleware/legacy-routes-redirect.ts @@ -180,6 +180,8 @@ const LEGACY_ROUTES = { "/solid-router/reference/response-helpers/revalidate": "/solid-router/reference/data-apis/revalidate", + + "/solid-start/guides/data-loading": "/solid-start/guides/data-fetching", } as const; function isLegacyRoute(path: string): path is keyof typeof LEGACY_ROUTES { diff --git a/src/routes/solid-router/concepts/actions.mdx b/src/routes/solid-router/concepts/actions.mdx index 53a96da9c..3d59eb63b 100644 --- a/src/routes/solid-router/concepts/actions.mdx +++ b/src/routes/solid-router/concepts/actions.mdx @@ -2,151 +2,445 @@ title: "Actions" --- -When developing applications, it is common to need to communicate new information to the server based on user interactions. -Actions are Solid Router’s solution to this problem. +Many user interactions in an application involve changing data on the server. +These **mutations** can be challenging to manage, as they require updates to the application's state and proper error handling. +Actions simplify managing data mutations. -## What are actions? +Actions provide several benefits: -Actions are asynchronous processing functions that allow you to submit data to your server and receive a response. -They are isomorphic, meaning they can run either on the server or the client, depending on what is needed. -This flexibility makes actions a powerful tool for managing and tracking data submissions. +- **Integrated state management:** + Solid Router automatically tracks the execution state of an action, simplifying reactive UI feedback. +- **Automatic data revalidation:** + After an action successfully completes, Solid Router revalidates relevant [`queries`](/solid-router/concepts/queries), ensuring the UI reflects the latest data. +- **Progressive enhancement:** + When used with HTML forms, actions enable functionality even if JavaScript is not yet loaded. -### How actions work +## Defining actions -Actions represent the server-side part of an [HTML form](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form). -They handle submissions through POST requests, allowing you to easily use HTML forms to send data. +Actions are defined by wrapping the data-mutation logic with the [`action` function](/solid-router/reference/data-apis/action). -When a user performs an action, such as submitting a form, the data is sent to the server for processing via an action. +```tsx +import { action } from "@solidjs/router"; + +const createTicketAction = action(async (subject: string) => { + const response = await fetch("https://my-api.com/support/tickets", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ subject }), + }); + + if (!response.ok) { + const errorData = await response.json(); + return { ok: false, message: errorData.message }; + } + + return { ok: true }; +}, "createTicket"); +``` + +In this example, an action is defined that creates a support ticket using a remote API. + +## Using actions + +Actions can be triggered in two ways: using a HTML [`
` element](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/form) or programmatically using the [`useAction` primitive](/solid-router/reference/data-apis/use-action). -### Benefits of using actions +The recommended approach is to use a `` element. +This ensures a robust user experience with progressive enhancement, since the form works even without JavaScript. -1. **Isomorphic**: Since actions can run on both the server and client, you can optimize performance by choosing the best execution environment for your needs. -2. **Asynchronous processing**: Actions handle data submissions asynchronously, ensuring that your application remains responsive. -3. **Simplified data handling**: By using actions, the process of managing and tracking data submissions can be streamlined, reducing the complexity of your application. +For cases where a form is not suitable, the [`useAction` primitive](/solid-router/reference/data-apis/use-action) can be used to trigger the action programmatically. -## Creating actions +### With the `` element -To create an action, use the `action` function from the `@solidjs/router` package. -This function takes an asynchronous function as an argument and returns a new function that can be used to submit data. +Solid Router extends the standard HTML `` element to work with actions. +Form submissions can be handled using action by passing an action to the `action` prop. + +Consider these points when using actions with ``: + +1. The `` element **must** have `method="post"`. +2. The action function will automatically receive the form's data as a [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) object as its first parameter. +3. For SSR environments, a unique name **must** be provided as the second parameter to the `action` function. + This name is used by Solid Router to identify and serialize the action across the client and server. ```tsx import { action } from "@solidjs/router"; -const echo = action(async (message: string) => { - // Simulates an asynchronous operation, such as an API call - await new Promise((resolve, reject) => setTimeout(resolve, 1000)); - console.log(message); -}); +const submitFeedbackAction = action(async (formData: FormData) => { + const message = formData.get("message")?.toString(); + // ... Sends the feedback to the server. +}, "submitFeedback"); + +function FeedbackForm() { + return ( + +