react-query-kit 1.0.3 → 1.0.4
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.
- package/README.md +23 -12
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -8,13 +8,15 @@
|
|
|
8
8
|
|
|
9
9
|
## The problem
|
|
10
10
|
|
|
11
|
-
- ReactQuery that needs
|
|
12
|
-
- ReactQuery
|
|
13
|
-
- ReactQuery
|
|
11
|
+
- ReactQuery that needs you manual manage your queryKey
|
|
12
|
+
- ReactQuery doesn't have an elegant way to set defaultOptions for per custom useQuery hook
|
|
13
|
+
- ReactQuery doesn't have a type-safe way to get the queryKey
|
|
14
14
|
|
|
15
15
|
## This solution
|
|
16
16
|
|
|
17
|
-
A simplest way to create
|
|
17
|
+
A simplest way to create your custom useQuery hooks without considering the consistence of the queryKey and provide default options to per hooks you create
|
|
18
|
+
|
|
19
|
+

|
|
18
20
|
|
|
19
21
|
## Table of Contents
|
|
20
22
|
|
|
@@ -56,6 +58,7 @@ type Variables = { id: number }
|
|
|
56
58
|
const usePost = createQuery<Response, Variables, Error>({
|
|
57
59
|
primaryKey: '/posts',
|
|
58
60
|
queryFn: ({ queryKey: [primaryKey, variables] }) => {
|
|
61
|
+
// primaryKey equals to '/posts'
|
|
59
62
|
return fetch(`${primaryKey}/${variables.id}`).then(res => res.json())
|
|
60
63
|
},
|
|
61
64
|
})
|
|
@@ -106,14 +109,14 @@ const queries = useQueries({
|
|
|
106
109
|
// setQueryData
|
|
107
110
|
queryClient.setQueryData(usePost.getKey(variables), {...})
|
|
108
111
|
|
|
109
|
-
// set data
|
|
112
|
+
// set data to all queries of `post`
|
|
110
113
|
queryClient.setQueriesData([usePost.getKey()], {...})
|
|
111
|
-
// or set data
|
|
114
|
+
// or set data to some speicial queries of `post`
|
|
112
115
|
queryClient.setQueriesData(usePost.getKey(variables), {...})
|
|
113
116
|
|
|
114
117
|
// invalidate all queries of `post`
|
|
115
118
|
queryClient.invalidateQueries(usePost.getKey())
|
|
116
|
-
// or invalidate speicial queries of `post`
|
|
119
|
+
// or invalidate some speicial queries of `post`
|
|
117
120
|
queryClient.invalidateQueries(usePost.getKey(variables))
|
|
118
121
|
```
|
|
119
122
|
|
|
@@ -122,7 +125,7 @@ queryClient.invalidateQueries(usePost.getKey(variables))
|
|
|
122
125
|
Options
|
|
123
126
|
- `primaryKey: string`
|
|
124
127
|
- Required
|
|
125
|
-
- `primaryKey` will be the first element of the
|
|
128
|
+
- `primaryKey` will be the first element of the array of `queryKey`
|
|
126
129
|
|
|
127
130
|
Returns
|
|
128
131
|
- `getPrimaryKey: () => primaryKey`
|
|
@@ -142,17 +145,17 @@ type Variables = { active: boolean }
|
|
|
142
145
|
|
|
143
146
|
const useProjects = createInfiniteQuery<Data, Variables, Error>({
|
|
144
147
|
primaryKey: 'projects',
|
|
145
|
-
queryFn: ({ queryKey: [
|
|
148
|
+
queryFn: ({ queryKey: [_primaryKey, variables], pageParam = 1 }) => {
|
|
146
149
|
return fetch(`/projects?cursor=${pageParam}?active=${variables.active}`).then(res => res.json())
|
|
147
150
|
},
|
|
148
151
|
getNextPageParam: (lastPage, pages) => lastPage.nextCursor,
|
|
149
152
|
})
|
|
150
153
|
|
|
151
|
-
const variables = {
|
|
154
|
+
const variables = { active: true }
|
|
152
155
|
|
|
153
156
|
// example
|
|
154
157
|
export default function Page() {
|
|
155
|
-
// queryKey equals to `['
|
|
158
|
+
// queryKey equals to `['projects', { active: true }]`
|
|
156
159
|
const { data, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage } =
|
|
157
160
|
useProjects({ suspense: true, variables })
|
|
158
161
|
|
|
@@ -312,4 +315,12 @@ a 👍. This helps maintainers prioritize what to work on.
|
|
|
312
315
|
|
|
313
316
|
## LICENSE
|
|
314
317
|
|
|
315
|
-
MIT
|
|
318
|
+
MIT
|
|
319
|
+
|
|
320
|
+
<!-- prettier-ignore-start -->
|
|
321
|
+
[npm]: https://www.npmjs.com
|
|
322
|
+
[node]: https://nodejs.org
|
|
323
|
+
[bugs]: https://github.com/liaoliao666/react-query-kit/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Acreated-desc+label%3Abug
|
|
324
|
+
[requests]: https://github.com/liaoliao666/react-query-kit/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3Aenhancement
|
|
325
|
+
[good-first-issue]: https://github.com/liaoliao666/react-query-kit/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3Aenhancement+label%3A%22good+first+issue%22
|
|
326
|
+
<!-- prettier-ignore-end -->
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-query-kit",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "A simplest way to create react-query without considering the consistence of queryKey or queryFn ",
|
|
5
5
|
"author": "liaoliao666",
|
|
6
6
|
"repository": "liaoliao666/react-query-kit",
|
|
@@ -77,5 +77,9 @@
|
|
|
77
77
|
"es",
|
|
78
78
|
"lib",
|
|
79
79
|
"types"
|
|
80
|
+
],
|
|
81
|
+
"keywords": [
|
|
82
|
+
"react",
|
|
83
|
+
"react query"
|
|
80
84
|
]
|
|
81
85
|
}
|