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
forms, user input, data mutations, optimistic updates, form submissions,
5
-
server actions, post requests
4
+
actions, form actions, mutations, submissions
6
5
tags:
7
6
- actions
8
7
- forms
9
8
- mutations
10
-
- post
11
-
- validation
12
-
- optimistic-updates
13
-
- server
9
+
- submissions
14
10
version: "1.0"
15
11
description: >-
16
-
Learn how to handle form submissions and data mutations in SolidJS with
17
-
actions, including optimistic updates and server-side processing.
12
+
action wraps an async function as a router action.
18
13
---
19
14
20
-
The `action` function wraps an asynchronous function and returns an [action](/solid-router/concepts/actions).
21
-
22
-
When an action is triggered, it creates a submission object that tracks its execution status.
23
-
This state can be accessed with the [`useSubmission`](/solid-router/reference/data-apis/use-submission) and [`useSubmissions`](/solid-router/reference/data-apis/use-submissions) primitives.
24
-
25
-
After an action completed successfully, all queries active in the same page are revalidated, unless revalidation is configured using [response helpers](/solid-router/concepts/actions#managing-navigation-and-revalidation).
15
+
`action` wraps an async function and returns an action function with router submission tracking. Matching submissions can be read with [`useSubmission`](/solid-router/reference/data-apis/use-submission) and [`useSubmissions`](/solid-router/reference/data-apis/use-submissions).
26
16
27
17
## Import
28
18
@@ -40,116 +30,121 @@ function action<T extends Array<any>, U = void>(
An asynchronous function that performs the action's logic.
62
-
When the action is used with a [`<form>` element](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/form), the function receives a [`FormData` object](https://developer.mozilla.org/en-US/docs/Web/API/FormData) as its first parameter.
47
+
Async function called when the action runs.
63
48
64
-
### `options`
49
+
When native form handling calls the action, the argument is `FormData` for `multipart/form-data` forms and `URLSearchParams` otherwise.
Function called after the action response is handled.
89
75
90
76
## Return value
91
77
92
-
`action` returns an action with the following properties.
78
+
`action` returns a function with the following properties:
79
+
80
+
### `url`
81
+
82
+
-**Type:**`string`
83
+
84
+
String used when the action is rendered as a form `action` and when submissions are matched back to this action. The optional `name` provides a stable value for this string.
93
85
94
86
### `with`
95
87
96
-
A method that wraps the original action and returns a new one.
97
-
When this new action is triggered, the arguments passed to `with` are forwarded to the original action.
98
-
If this new action is used with a `<form>`, the `FormData` object is passed as the last parameter, after any other forwarded parameters.
88
+
-**Type:**`(...args: any[]) => Action<any[], U>`
89
+
90
+
Function that creates an action with leading arguments prefilled.
91
+
92
+
## Behavior
93
+
94
+
- Calling an action adds a submission to the router submissions signal.
95
+
- Each submission exposes `input`, `url`, `result`, `error`, `pending`, `clear`, and `retry`.
96
+
-`with` creates another action that calls the original action with leading arguments already supplied.
97
+
-`onComplete` receives a submission snapshot after the response is handled.
98
+
-`Response` objects with an `X-Revalidate` header supply the keys passed to `revalidate`; without that header, action response handling passes `undefined`.
99
+
- Returned `Response` objects with a `Location` header trigger client navigation to that location.
100
+
- On the client, actions are registered by URL in the action map and removed during cleanup when an owner exists.
101
+
- If an action has no URL, converting it to a string throws.
api calls, data fetching, deduplication, preloading routes, caching responses,
6
-
preventing waterfalls
5
+
deprecated query alias, cached functions, route data
7
6
tags:
8
7
- cache
9
8
- deprecated
10
-
- data-fetching
11
-
- deduplication
12
-
- preload
9
+
- query
13
10
version: "1.0"
14
11
description: >-
15
-
Deprecated caching API for deduplicating requests and preloading data. Use
16
-
query instead for better performance and invalidation.
12
+
cache is a deprecated alias for query.
17
13
---
18
14
19
-
:::caution[Deprecation Warning]
20
-
This API is deprecated since `v0.15.0` of Solid Router. Use [query](/solid-router/reference/data-apis/query) instead. It will be removed in an upcoming release.
21
-
:::
15
+
`cache` is a deprecated alias for [`query`](/solid-router/reference/data-apis/query).
22
16
23
-
`cache` is a [higher-order function](https://en.wikipedia.org/wiki/Higher-order_function) designed to create a new function with the same signature as the function passed to it.
24
-
When this newly created function is called for the first time with a specific set of arguments, the original function is run, and its return value is stored in a cache and returned to the caller of the created function.
25
-
The next time the created function is called with the same arguments (as long as the cache is still valid), it will return the cached value instead of re-executing the original function.
17
+
## Import
26
18
27
-
:::note
28
-
`cache` can be defined anywhere and then used inside your components with [`createAsync`](/solid-router/reference/data-apis/create-async).
29
-
30
-
However, using `cache` directly in [`createResource`](/reference/basic-reactivity/create-resource) will not work since the fetcher is not reactive and will not invalidate properly.
1. Deduping on the server for the lifetime of the request.
90
-
2. Preloading the cache in the browser - this lasts 5 seconds.
91
-
When a route is preloaded on hover or when preload is called when entering a route it will make sure to dedupe calls.
92
-
3. A reactive refetch mechanism based on key.
93
-
This prevents routes that are not new from retriggering on action revalidation.
94
-
4. Serve as a back/forward cache for browser navigation for up to 5 minutes.
95
-
Any user based navigation or link click bypasses it.
96
-
Upon revalidation or new fetch the cache is updated.
97
-
98
-
## Cache keys
29
+
## Parameters
99
30
100
-
To ensure that the cache keys are consistent and unique, arguments are deterministically serialized using JSON.stringify.
101
-
Before serialization, key/value pairs in objects are sorted so that the order of properties does not affect the serialization.
102
-
For instance, both `{ name: 'Ryan', awesome: true }` and `{ awesome: true, name: 'Ryan' }` will serialize to the same string so that they produce the same cache key.
31
+
`cache` has the same parameters as [`query`](/solid-router/reference/data-apis/query).
103
32
104
33
## Return value
105
34
106
-
The return value is a `CachedFunction`, a function that has the same signature as the function you passed to `cache`.
107
-
This cached function stores the return value using the cache key.
108
-
Under most circumstances, this temporarily prevents the passed function from running with the same arguments, even if the created function is called repeatedly.
35
+
-**Type:**`CachedFunction<T>`
109
36
110
-
## Arguments
37
+
`cache` returns the same value as [`query`](/solid-router/reference/data-apis/query).
0 commit comments