You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/routes/solid-router/concepts/actions.mdx
+62-39Lines changed: 62 additions & 39 deletions
Original file line number
Diff line number
Diff line change
@@ -2,64 +2,74 @@
2
2
title: "Actions"
3
3
---
4
4
5
-
To communicate with your server, you will need to use actions.
6
-
Actions are a way to submit data to your server and receive a response.
7
-
They are isomorphic, meaning they can run on the server or the client.
5
+
When developing applications, it is common to need to communicate new information to the server based on user interactions.
6
+
Actions are Solid Routers solution to this problem.
8
7
9
-
One question you will likely have when developing any sort of app is "how do I communicate new information to my server?".
10
-
The user did something.
11
-
What next? Solid Router's answer to this is _actions_.
8
+
## What are actions?
12
9
13
-
Actions give you the ability to specify an async action processing function and gives you elegant tools to help you easily manage and track submissions. Actions are isomorphic and represent a `POST` request.
10
+
Actions are asynchronous processing functions that allow you to submit data to your server and receive a response.
11
+
They are isomorphic, meaning they can run either on the server or the client, depending on what is needed.
12
+
This flexibility makes actions a powerful tool for managing and tracking data submission.
14
13
15
-
Actions are isomorphic. This means that a submission can be handled on the server _or_ the client, whichever is optimal. They represent the server component of an [HTML form](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form), and even help you use HTML forms to submit data.
14
+
### How actions work
15
+
16
+
Actions represent the server-side part of an [HTML form](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form).
17
+
They handle submissions through POST requests, allowing you to easily use HTML forms to send data.
18
+
19
+
When a user performs an action, such as submitting a form, the data is sent to the server for processing via an action.
20
+
21
+
### Benefits of using actions
22
+
23
+
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.
24
+
2.**Asynchronous processing**: Actions handle data submissions asynchronously, ensuring that your application remains responsive.
25
+
3.**Simplified data handling**: By using actions, the process of managing and tracking data submissions can be streamlined, reducing the complexity of your application.
16
26
17
27
## Creating actions
18
28
19
-
Let's stop getting ahead of ourselves! First, let's create an action!
29
+
To create an action, use the `action` function from the `@solidjs/router` package.
30
+
This function takes an asynchronous function as an argument and returns a new function that can be used to submit data.
This `echo` action will act as your backend, however you can substitute it for any API, provided you are ok with it running on the client. Typically, route actions are used with some sort of solution like fetch or GraphQL.
42
+
In this example, the `echo` action simulates a fetch call with a 1 second delay before logging the message to the console.
43
+
The `echo` action will act as a backend, however, it can be substituted for any API provided it can be run on the client.
44
+
Typically, route actions are used with some sort of solution like fetch or GraphQL.
36
45
37
-
These will return either a `Response` such as a redirect (we are not returning anything quite yet!) or any value. If you want to ensure the action only runs on the server for things like databases, you will want to use `"use server"`, introduced below.
46
+
### Using actions
38
47
39
-
Naturally, this action won't do anything quite yet. We still need to call it somewhere! For now, let's call it manually from some component using the submit function returned from `action`.
48
+
To use the action, you can call it from within a component using `useAction`.
49
+
This returns a function that can be called with the necessary arguments to trigger the action.
You should see `Hello from solid!` back in the console!
61
+
In this component, `useAction` is used to get a reference to the `echo` action.
62
+
The action is then called with the message `"Hello from Solid!"`, which will be logged to the console after a 1 second delay.
63
+
64
+
### Returning data from actions
57
65
58
-
### Returning from actions
66
+
In many cases, after submitting data, the server sends some data back as well.
67
+
This may be in the form of an error message if something has failed or the results of a successful operation.
68
+
Anything returned from an action can be accessed using the reactive `action.result` property, where the value can change each time you submit your action.
59
69
60
-
In many cases, after submitting data the server sends some data back as well. Usually an error message if something failed. Anything returned from your action function can be accessed using the reactive `action.result` property. The value of this property can change each time you submit your action.
70
+
To access the action's result, you must pass the action to `useSubmission`:
setTimeout(() =>myEcho("This is a second submission!"), 1500);
87
+
75
88
return <p>{echoing.result}</p>;
76
89
}
77
90
```
78
91
79
-
While this method of using actions works, it leaves the implementation details of how you trigger `echo` up to you. When handling explicit user input, it's better to use a `form` for a multitude of reasons.
92
+
Using `useSubmission` leaves the implementation details of how you trigger `echo` up to you.
93
+
Whe handling user inputs, for example, it is better to use a `form` for a multitude of reasons.
80
94
81
95
## Using forms to submit data
82
96
83
-
We highly recommend using HTML forms as your method to submit data with actions. HTML forms can be used even before JavaScript loads, leading to instantly interactive applications.
97
+
When submitting data with actions, it is recommended to use HTML forms.
98
+
These forms can be used prior to JavaSCript loading, which creates instantly interactive applications.
99
+
This also inherently provides accessibility benefits, saving the time of designing a custom UI library that may not have these benefits.
84
100
85
-
They have the added benefit of implicit accessibility. They can save you valuable time that would have otherwise been spent designing a UI library that will never have the aforementioned benefits.
101
+
When using forms to submit actions, the first argument passed to your action function is an instance of [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData).
102
+
To use actions with forms, pass the action to the `action` property of your form.
103
+
This creates progressively enhanced forms that work even when JavaScript is disabled.
86
104
87
-
When forms are used to submit actions, the first argument is an instance of [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData). To write a form using actions, pass the action to the action property of your form. You can then walk away with amazing, progressively enhanced forms!
105
+
If you do not return a `Response` from your action, the user will stay on the same page and responses will be re-triggered.
106
+
Using a `redirect` can tell the browser to navigate to a new page.
88
107
89
-
If you don't return a `Response` from your action, the user will stay on the same page and your resources will be re-triggered. You can also throw a `redirect` to tell the browser to navigate.
@@ -111,6 +132,8 @@ export function MyComponent() {
111
132
112
133
## Error handling
113
134
114
-
We strongly recommend with actions to "return" errors rather than throwing them. This can help with typing of submissions you'd use with `useSubmission`. This is important especially for handling progressive enhancement where no JS is present in the client so that we can use the error declaratively to render the updated page on the server.
135
+
Rather than throwing errors, it is recommended to return them from actions.
136
+
This helps with the typing of submissions that would be used with `useSubmission`.
137
+
This is important when handling progressive enhancement where no JavaScript is present in the client, so that errors can be used declaratively to render the updated page on the server.
115
138
116
-
Additionally when using Server Actions it is good practice to try to handle errors on the server so that you can sanitize error messages.
139
+
Additionally, when using server actions, it is good practice to handle errors on the server to sanitize error messages.
0 commit comments