@@ -35,36 +35,40 @@ Following code loads a list of Posts from an API, and presents a Delete button t
3535When 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
4440type 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
5659export 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}
0 commit comments