Skip to content

Commit 904471b

Browse files
Merge branch 'main' into docs/rendering-modes
2 parents 1cc0fb9 + b4b9cf2 commit 904471b

File tree

1 file changed

+76
-24
lines changed

1 file changed

+76
-24
lines changed

src/routes/solid-router/reference/data-apis/revalidate.mdx

Lines changed: 76 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,100 @@ tags:
1010
- invalidation
1111
- polling
1212
- refetch
13-
version: '1.0'
13+
version: "1.0"
1414
description: >-
1515
Manually revalidate cached queries to refresh stale data, implement polling,
1616
or trigger updates after mutations in SolidJS.
1717
---
1818

19-
The `revalidate` function is used to revalidate queries associated with specified [query keys](/solid-router/reference/data-apis/query#query-keys).
20-
When a [query](/solid-router/reference/data-apis/query) is revalidated, it is executed again, and any references to the associated query data are updated accordingly.
19+
The `revalidate` function triggers revalidation of [queries](/solid-router/data-fetching/queries) by their keys.
20+
Each query with active subscribers re-executes and updates its dependents; queries without subscribers are marked stale but don't execute until subscribed.
21+
22+
## Import
23+
24+
```tsx
25+
import { revalidate } from "@solidjs/router";
26+
```
27+
28+
## Type
29+
30+
```tsx
31+
function revalidate(
32+
key?: string | string[] | void,
33+
force?: boolean
34+
): Promise<void>;
35+
```
36+
37+
## Parameters
38+
39+
### `key`
40+
41+
- **Type:** `string | string[] | void`
42+
- **Required:** No
43+
44+
The query key or array of query keys to revalidate.
45+
If not provided, all queries on the current page are revalidated.
46+
47+
### `force`
48+
49+
- **Type:** `boolean`
50+
- **Required:** No
51+
- **Default:** `true`
52+
53+
When `true`, clears the internal cache used for deduplication.
54+
When `false`, allows cached data to be reused if available.
55+
56+
## Return value
57+
58+
`revalidate` returns a `Promise` that resolves when the revalidation transition completes.
59+
60+
## Examples
61+
62+
### Basic usage
2163

2264
```tsx
23-
import { For } from "solid-js";
2465
import { query, createAsync, revalidate } from "@solidjs/router";
2566

26-
const getPosts = query(async () => {
27-
return await fetch("https://api.com/posts").then((response) =>
28-
response.json()
29-
);
30-
}, "posts");
67+
const getUserQuery = query(async () => {
68+
// ... Fetches user data.
69+
return { name: "John" };
70+
}, "user");
3171

32-
function Posts() {
33-
const posts = createAsync(() => getPosts());
72+
function UserProfile() {
73+
const user = createAsync(() => getUserQuery());
3474

35-
function refetchPosts() {
36-
revalidate(getPosts.key);
75+
function refreshUser() {
76+
revalidate(getUserQuery.key);
3777
}
3878

3979
return (
4080
<div>
41-
<button onClick={refetchPosts}>Refetch posts</button>
42-
<ul>
43-
<For each={posts()}>{(post) => <li>{post.title}</li>}</For>
44-
</ul>
81+
<button onClick={refreshUser}>Refresh</button>
82+
<p>{user()?.name}</p>
4583
</div>
4684
);
4785
}
4886
```
4987

50-
## Parameters
88+
### Revalidating multiple queries
89+
90+
```tsx
91+
import { query, revalidate } from "@solidjs/router";
92+
93+
const getUsersQuery = query(async () => {
94+
// ... Fetches users.
95+
}, "users");
96+
97+
const getPostsQuery = query(async () => {
98+
// ... Fetches posts.
99+
}, "posts");
100+
101+
function refreshAll() {
102+
revalidate([getUsersQuery.key, getPostsQuery.key]);
103+
}
104+
```
105+
106+
## Related
51107

52-
- `key`: The query key or an array of query keys to be revalidated.
53-
- `force` (optional): A boolean that indicates whether to delete the existing cached value of the queries.
54-
Note that this cache is solely for de-duplication purposes.
55-
Therefore, deleting the cache only affects de-duplication.
56-
For more information on how `query` works, refer to [the `query` documentation](/solid-router/reference/data-apis/query).
57-
The default value is `true`.
108+
- [`query`](/solid-router/reference/data-apis/query)
109+
- [`createAsync`](/solid-router/reference/data-apis/create-async)

0 commit comments

Comments
 (0)