Skip to content

Commit e1a927e

Browse files
committed
update readme example
1 parent 18e65a1 commit e1a927e

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,36 +35,40 @@ Following code loads a list of Posts from an API, and presents a Delete button t
3535
When a Post is deleted, the list query is automatically invalidated and refetched to show the up-to-date data.
3636

3737
```tsx
38-
import { HttpMutationBuilder, HttpQueryBuilder } from "react-query-builder";
39-
40-
const baseUrl = "https://jsonplaceholder.typicode.com";
41-
const baseQuery = new HttpQueryBuilder().withBaseUrl(baseUrl);
42-
const baseMutation = new HttpMutationBuilder().withBaseUrl(baseUrl);
38+
import { HttpQueryBuilder } from "react-query-builder";
4339

4440
type PostData = { id: number; title: string; body: string; userId: number };
4541

46-
const postsQuery = baseQuery
42+
const builder = new HttpQueryBuilder()
43+
.withBaseUrl("https://jsonplaceholder.typicode.com")
44+
.withTagTypes<{
45+
posts: PostData[];
46+
refreshable: unknown;
47+
}>();
48+
49+
const postsQuery = builder
4750
.withTags("refreshable", "posts")
4851
.withPath("/posts")
4952
.withData<PostData[]>();
5053

51-
const deletePostMutation = baseMutation
54+
const deletePostMutation = builder
5255
.withUpdates("posts")
5356
.withMethod("delete")
5457
.withPath("/posts/:id");
5558

5659
export function MyApp() {
60+
const [refresh] = builder.tags.useOperation({ tags: "refreshable" });
5761
const posts = postsQuery.useQuery({});
5862
const deletePost = deletePostMutation.useMutation();
5963

60-
const [refresh] = useOperateOnTags({ tags: "refreshable" });
61-
6264
const onDelete = (id: number) => deletePost.mutateAsync({ params: { id } });
6365

6466
if (!posts.isSuccess) return <>Loading...</>;
6567

6668
return (
6769
<>
70+
<button onClick={() => refresh()}>Refresh all posts</button>
71+
6872
{posts.data.map((post) => (
6973
<div key={post.id}>
7074
<h2>{post.title}</h2>
@@ -78,8 +82,6 @@ export function MyApp() {
7882
</button>
7983
</div>
8084
))}
81-
82-
<button onClick={() => refresh()}>Refresh all posts</button>
8385
</>
8486
);
8587
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { HttpQueryBuilder } from 'react-query-builder';
2+
3+
type PostData = { id: number; title: string; body: string; userId: number };
4+
5+
const builder = new HttpQueryBuilder().withBaseUrl('https://jsonplaceholder.typicode.com').withTagTypes<{
6+
posts: PostData[];
7+
refreshable: unknown;
8+
}>();
9+
10+
const postsQuery = builder.withTags('refreshable', 'posts').withPath('/posts').withData<PostData[]>();
11+
12+
const deletePostMutation = builder.withUpdates('posts').withMethod('delete').withPath('/posts/:id');
13+
14+
export function MyApp() {
15+
const [refresh] = builder.tags.useOperation({ tags: 'refreshable' });
16+
const posts = postsQuery.useQuery({});
17+
const deletePost = deletePostMutation.useMutation();
18+
19+
const onDelete = (id: number) => deletePost.mutateAsync({ params: { id } });
20+
21+
if (!posts.isSuccess) return <>Loading...</>;
22+
23+
return (
24+
<>
25+
<button onClick={() => refresh()}>Refresh all posts</button>
26+
27+
{posts.data.map((post) => (
28+
<div key={post.id}>
29+
<h2>{post.title}</h2>
30+
<p>{post.body}</p>
31+
32+
<button onClick={() => onDelete(post.id)} disabled={deletePost.isPending}>
33+
Delete
34+
</button>
35+
</div>
36+
))}
37+
</>
38+
);
39+
}

0 commit comments

Comments
 (0)