@tramvai/react-query 1.8.3 → 1.8.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.en.md +194 -0
  2. package/package.json +4 -4
package/README.en.md ADDED
@@ -0,0 +1,194 @@
1
+ # React Query
2
+
3
+ A library for handling requests in React components. Based on [react-query](https://react-query.tanstack.com/).
4
+
5
+ ## Explanation
6
+
7
+ For the library to work, the module [@tramvai/module-react-query](references/modules/react-query.md) must be added to the tramvai application
8
+
9
+ ## Api
10
+
11
+ ### createQuery
12
+
13
+ Allows you to create a `Query` object that can later be used in components using `useQuery`. Used to execute single data retrieval requests.
14
+
15
+ ```ts
16
+ import { createQuery } from '@tramvai/react-query';
17
+
18
+ const query = createQuery({
19
+ key: 'base',
20
+ fn: async (_, { apiClient }) => {
21
+ const { payload } = await apiClient.get('api/base');
22
+
23
+ return payload;
24
+ },
25
+ deps: {
26
+ apiClient: TINKOFF_API_SERVICE,
27
+ },
28
+ });
29
+ ```
30
+
31
+ #### Unique query parameters
32
+
33
+ To create a generic `query` that takes parameters for a query, you must return a unique `key`,
34
+ you can read more about this in the official documentation section [Query Keys](https://react-query.tanstack.com/guides/query-keys)
35
+
36
+ As a parameter `key` you can use:
37
+
38
+ - a string, such as `key: 'query-name'`
39
+ - an array where any serializable data can be used as elements, for example `key: ['query-name', false, { bar: 'baz }]`
40
+ - a function that takes the parameters with which `query` is called and returns a string - `key: (options) => 'query-name'`
41
+ - a function that accepts parameters, with which `query` is called, and returns an array, where any serializable data can be used as elements - `key: (options) => ['query-name', options, { bar: 'baz' }]`
42
+
43
+ ```ts
44
+ import { createQuery, useQuery } from '@tramvai/react-query';
45
+
46
+ const query = createQuery({
47
+ key: (id: number) => ['user', id],
48
+ fn: async (id, { apiClient }) => {
49
+ const { payload } = await apiClient.get(`api/user/${id}`);
50
+
51
+ return payload;
52
+ },
53
+ deps: {
54
+ apiClient: TINKOFF_API_SERVICE,
55
+ },
56
+ });
57
+
58
+ export function Component({ id }) {
59
+ const { data, isLoading } = useQuery(query, id);
60
+
61
+ return <div>{isLoading ? 'loading...' : data}</div>;
62
+ }
63
+ ```
64
+
65
+ ### useQuery
66
+
67
+ React hook for working with `Query` object
68
+
69
+ [Документация из react-query](https://react-query.tanstack.com/reference/useQuery)
70
+
71
+ ```ts
72
+ import { useQuery } from '@tramvai/react-query';
73
+
74
+ export function Component() {
75
+ const { data, isLoading } = useQuery(query);
76
+
77
+ return <div>{isLoading ? 'loading...' : data}</div>;
78
+ }
79
+ ```
80
+
81
+ ### createInfiniteQuery
82
+
83
+ Creates an `InfiniteQuery` object that can later be used in components using `useInfiniteQuery`. It is used to execute queries to obtain a sequence of data that can be loaded as the component runs.
84
+
85
+ ```ts
86
+ import { createInfiniteQuery } from '@tramvai/react-query';
87
+
88
+ const query = createInfiniteQuery({
89
+ key: 'list',
90
+ fn: async (_, start = 0, { apiClient }) => {
91
+ const { payload } = await apiClient.get<Response>('api/list', {
92
+ query: {
93
+ count: 30,
94
+ start,
95
+ },
96
+ });
97
+
98
+ return payload;
99
+ },
100
+ getNextPageParam: (page: Response) => {
101
+ return page.nextPage;
102
+ },
103
+ deps: {
104
+ apiClient: TINKOFF_API_SERVICE,
105
+ },
106
+ });
107
+ ```
108
+
109
+ ### useInfiniteQuery
110
+
111
+ React hook for working with the `InfiniteQuery` object
112
+
113
+ [Документация из react-query](https://react-query.tanstack.com/reference/useInfiniteQuery)
114
+
115
+ ```ts
116
+ import { useInfiniteQuery } from '@tramvai/react-query';
117
+
118
+ export function Component() {
119
+ const { data, isLoading, fetchNextPage, hasNextPage } = useInfiniteQuery(query);
120
+
121
+ if (isLoading) {
122
+ return 'loading...';
123
+ }
124
+
125
+ return (
126
+ <div>
127
+ <div>
128
+ {data.pages.map((page) => {
129
+ return page.list.map((entry) => {
130
+ return <div key={entry}>{entry}</div>;
131
+ });
132
+ })}
133
+ </div>
134
+ {hasNextPage && (
135
+ <button type="button" onClick={() => fetchNextPage()}>
136
+ Load more
137
+ </button>
138
+ )}
139
+ </div>
140
+ );
141
+ }
142
+ ```
143
+
144
+ ### createMutation
145
+
146
+ Creates a `Mutation` object that can later be used in components using `useMutation`. Used to send and modify data in the api.
147
+
148
+ ```ts
149
+ import { createMutation } from '@tramvai/react-query';
150
+
151
+ const mutation = createMutation({
152
+ key: 'post',
153
+ fn: async (_, data: string, { apiClient }) => {
154
+ const { payload } = await apiClient.post('api/post', {
155
+ body: {
156
+ data,
157
+ },
158
+ });
159
+
160
+ return payload;
161
+ },
162
+ deps: {
163
+ apiClient: TINKOFF_API_SERVICE,
164
+ },
165
+ });
166
+ ```
167
+
168
+ ### useMutation
169
+
170
+ React hook for working with the `Mutation` object
171
+
172
+ [Документация из react-query](https://react-query.tanstack.com/reference/useMutation)
173
+
174
+ ```ts
175
+ import { useMutation } from '@tramvai/react-query';
176
+
177
+ export function Component() {
178
+ const { isLoading, mutate } = useMutation(mutation);
179
+
180
+ if (isLoading) {
181
+ return 'loading...';
182
+ }
183
+
184
+ return (
185
+ <button type="button" onClick={() => mutate('test')}>
186
+ Send data
187
+ </button>
188
+ );
189
+ }
190
+ ```
191
+
192
+ ## How-to
193
+
194
+ [Examples of using @tramvai/react-query](how-to/react-query-usage.md)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/react-query",
3
- "version": "1.8.3",
3
+ "version": "1.8.7",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
@@ -18,9 +18,9 @@
18
18
  "build-for-publish": "true"
19
19
  },
20
20
  "dependencies": {
21
- "@tramvai/core": "1.8.3",
22
- "@tramvai/module-react-query": "1.8.3",
23
- "@tramvai/state": "1.8.3"
21
+ "@tramvai/core": "1.8.7",
22
+ "@tramvai/module-react-query": "1.8.7",
23
+ "@tramvai/state": "1.8.7"
24
24
  },
25
25
  "peerDependencies": {
26
26
  "@tinkoff/dippy": "0.7.35",