@tramvai/react-query 2.72.3 → 2.72.5

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.md +2 -270
  2. package/package.json +5 -5
package/README.md CHANGED
@@ -3,6 +3,8 @@ title: '@tramvai/react-query'
3
3
  sidebar_position: 5
4
4
  ---
5
5
 
6
+ Link to complete React Query documentation - https://tramvai.dev/docs/features/data-fetching/react-query/
7
+
6
8
  # React Query
7
9
 
8
10
  A library for handling requests in React components. Based on [@tanstack/react-query](https://tanstack.com/query/v4/).
@@ -10,273 +12,3 @@ A library for handling requests in React components. Based on [@tanstack/react-q
10
12
  ## Explanation
11
13
 
12
14
  For the library to work, the module [@tramvai/module-react-query](references/modules/react-query.md) must be added to the tramvai application
13
-
14
- ## Api
15
-
16
- ### Query
17
-
18
- A wrapper around react-query options with tramvai integration.
19
-
20
- #### fork
21
-
22
- Create new `Query` from an existing query with option to override settings.
23
-
24
- ```ts
25
- import { createQuery } from '@tramvai/react-query';
26
-
27
- const query = createQuery();
28
- const newQuery = query.fork({
29
- refetchInterval: 2000,
30
- refetchIntervalInBackground: false,
31
- });
32
- ```
33
-
34
- #### prefetchAction
35
-
36
- Return a tramvai action that can be used to prefetch current query
37
-
38
- ```ts
39
- export default function PageComponent() {
40
- const { data, isLoading } = useQuery(query);
41
-
42
- return <div>{isLoading ? 'loading...' : data}</div>;
43
- }
44
-
45
- Component.actions = [query.prefetchAction()];
46
- ```
47
-
48
- #### fetchAction
49
-
50
- Return a tramvai action that can be used to fetch current query
51
-
52
- ```ts
53
- const action = declareAction({
54
- name: 'action',
55
- async fn() {
56
- const result = await this.executeAction(query.fetchAction());
57
-
58
- console.log('__action__', result);
59
- },
60
- });
61
- ```
62
-
63
- #### raw
64
-
65
- Might be used when the raw query options is needed. The result can be passed to the underlying methods of `react-query` lib in cases when `@tramvai/react-query` doesn't provide appropriate wrapper. This method is used internally in the lib to redirect calls to `react-query`.
66
-
67
- ### createQuery
68
-
69
- Allows you to create a `Query` object that can later be used in components using `useQuery`. Used to execute single data retrieval requests.
70
-
71
- ```ts
72
- import { createQuery } from '@tramvai/react-query';
73
-
74
- const query = createQuery({
75
- key: 'base',
76
- fn: async (_, { apiClient }) => {
77
- const { payload } = await apiClient.get('api/base');
78
-
79
- return payload;
80
- },
81
- deps: {
82
- apiClient: TINKOFF_API_SERVICE,
83
- },
84
- });
85
- ```
86
-
87
- #### Unique query parameters
88
-
89
- To create a generic `query` that takes parameters for a query, you must return a unique `key`, you can read more about this in the official documentation section [Query Keys](https://tanstack.com/query/v4/docs/guides/query-keys)
90
-
91
- As a parameter `key` you can use:
92
-
93
- - a string, such as `key: 'query-name'`
94
- - an array where any serializable data can be used as elements, for example `key: ['query-name', false, { bar: 'baz }]`
95
- - a function that takes the parameters with which `query` is called and returns a string - `key: (this: { deps }, options) => 'query-name'`. Where through `this.deps` you can get resolved deps for the query.
96
- - a function that accepts parameters, with which `query` is called, and returns an array, where any serializable data can be used as elements - `key: (this: { deps }, options) => ['query-name', options, { bar: 'baz' }]`
97
-
98
- ```ts
99
- import { createQuery, useQuery } from '@tramvai/react-query';
100
-
101
- const query = createQuery({
102
- key: (id: number) => ['user', id],
103
- async fn(id) {
104
- const { apiClient } = this.deps;
105
- const { payload } = await apiClient.get(`api/user/${id}`);
106
-
107
- return payload;
108
- },
109
- deps: {
110
- apiClient: TINKOFF_API_SERVICE,
111
- },
112
- });
113
-
114
- export function Component({ id }) {
115
- const { data, isLoading } = useQuery(query, id);
116
-
117
- return <div>{isLoading ? 'loading...' : data}</div>;
118
- }
119
- ```
120
-
121
- ### useQuery
122
-
123
- React hook for working with `Query` object
124
-
125
- [react-query docs](https://tanstack.com/query/v4/docs/reference/useQuery)
126
-
127
- ```ts
128
- import { useQuery } from '@tramvai/react-query';
129
-
130
- export function Component() {
131
- const { data, isLoading } = useQuery(query);
132
-
133
- return <div>{isLoading ? 'loading...' : data}</div>;
134
- }
135
- ```
136
-
137
- ### useQueries
138
-
139
- React Hook for working with the list of `Query` objects
140
-
141
- [react-query docs](https://tanstack.com/query/v4/docs/reference/useQueries)
142
-
143
- ```ts
144
- import { useQueries } from '@tramvai/react-query';
145
-
146
- export function Component() {
147
- const [{ data: data1, isLoading: isLoading1 }, { data: data2, isLoading: isLoading2 }] =
148
- useQueries([query1, query2]);
149
-
150
- return (
151
- <div>
152
- <div>{isLoading1 ? 'loading1...' : data1}</div>
153
- <div>{isLoading2 ? 'loading2...' : data2}</div>
154
- </div>
155
- );
156
- }
157
- ```
158
-
159
- ### createInfiniteQuery
160
-
161
- 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.
162
-
163
- ```ts
164
- import { createInfiniteQuery } from '@tramvai/react-query';
165
-
166
- const query = createInfiniteQuery({
167
- key: 'list',
168
- async fn(_, start = 0) {
169
- const { apiClient } = this.deps;
170
- const { payload } = await apiClient.get<Response>('api/list', {
171
- query: {
172
- count: 30,
173
- start,
174
- },
175
- });
176
-
177
- return payload;
178
- },
179
- getNextPageParam: (page: Response) => {
180
- return page.nextPage;
181
- },
182
- deps: {
183
- apiClient: TINKOFF_API_SERVICE,
184
- },
185
- });
186
- ```
187
-
188
- ### useInfiniteQuery
189
-
190
- React hook for working with the `InfiniteQuery` object
191
-
192
- [react-query docs](https://tanstack.com/query/v4/docs/reference/useInfiniteQuery)
193
-
194
- ```ts
195
- import { useInfiniteQuery } from '@tramvai/react-query';
196
-
197
- export function Component() {
198
- const { data, isLoading, fetchNextPage, hasNextPage } = useInfiniteQuery(query);
199
-
200
- if (isLoading) {
201
- return 'loading...';
202
- }
203
-
204
- return (
205
- <div>
206
- <div>
207
- {data.pages.map((page) => {
208
- return page.list.map((entry) => {
209
- return <div key={entry}>{entry}</div>;
210
- });
211
- })}
212
- </div>
213
- {hasNextPage && (
214
- <button type="button" onClick={() => fetchNextPage()}>
215
- Load more
216
- </button>
217
- )}
218
- </div>
219
- );
220
- }
221
- ```
222
-
223
- ### createMutation
224
-
225
- Creates a `Mutation` object that can later be used in components using `useMutation`. Used to send and modify data in the api.
226
-
227
- ```ts
228
- import { createMutation } from '@tramvai/react-query';
229
-
230
- const mutation = createMutation({
231
- key: 'post',
232
- async fn(_, data: string) {
233
- const { apiClient } = this.deps;
234
- const { payload } = await apiClient.post('api/post', {
235
- body: {
236
- data,
237
- },
238
- });
239
-
240
- return payload;
241
- },
242
- deps: {
243
- apiClient: TINKOFF_API_SERVICE,
244
- },
245
- });
246
- ```
247
-
248
- ### useMutation
249
-
250
- React hook for working with the `Mutation` object
251
-
252
- [react-query docs](https://tanstack.com/query/v4/docs/reference/useMutation)
253
-
254
- ```ts
255
- import { useMutation } from '@tramvai/react-query';
256
-
257
- export function Component() {
258
- const { isLoading, mutate } = useMutation(mutation);
259
-
260
- if (isLoading) {
261
- return 'loading...';
262
- }
263
-
264
- return (
265
- <button type="button" onClick={() => mutate('test')}>
266
- Send data
267
- </button>
268
- );
269
- }
270
- ```
271
-
272
- ## How-to
273
-
274
- [Examples of using @tramvai/react-query](how-to/react-query-usage.md)
275
-
276
- ### Use `react-query` directly
277
-
278
- :::warning Prefer to use methods from the `@tramvai/react-query` as it is can work both with the `Query` wrapper and the query options to `react-query` itself :::
279
-
280
- You can get [`QueryClient`](https://tanstack.com/query/v4/docs/reference/QueryClient) from di by token `QUERY_CLIENT_TOKEN` or using method `useQueryClient` in React-components.
281
-
282
- To convert wrapped `Query` object to object acceptable by `react-query` use method [raw](#raw) of the `Query` instance.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/react-query",
3
- "version": "2.72.3",
3
+ "version": "2.72.5",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
@@ -18,10 +18,10 @@
18
18
  },
19
19
  "dependencies": {
20
20
  "@tinkoff/react-hooks": "0.1.5",
21
- "@tramvai/core": "2.72.3",
22
- "@tramvai/module-react-query": "2.72.3",
23
- "@tramvai/react": "2.72.3",
24
- "@tramvai/tokens-common": "2.72.3"
21
+ "@tramvai/core": "2.72.5",
22
+ "@tramvai/module-react-query": "2.72.5",
23
+ "@tramvai/react": "2.72.5",
24
+ "@tramvai/tokens-common": "2.72.5"
25
25
  },
26
26
  "peerDependencies": {
27
27
  "@tinkoff/dippy": "0.8.13",