Skip to content

Commit 0b54fec

Browse files
committed
2 parents 69d4e94 + a0a7023 commit 0b54fec

File tree

5 files changed

+102
-35
lines changed

5 files changed

+102
-35
lines changed

.all-contributorsrc

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"projectName": "react-query",
3+
"projectOwner": "tannerlinsley",
4+
"repoType": "github",
5+
"repoHost": "https://github.com",
6+
"files": [
7+
"README.md"
8+
],
9+
"imageSize": 100,
10+
"commit": false,
11+
"commitConvention": "none",
12+
"contributors": [
13+
{
14+
"login": "tannerlinsley",
15+
"name": "Tanner Linsley",
16+
"avatar_url": "https://avatars0.githubusercontent.com/u/5580297?v=4",
17+
"profile": "https://tannerlinsley.com",
18+
"contributions": [
19+
"code",
20+
"ideas",
21+
"example",
22+
"maintenance",
23+
"review"
24+
]
25+
},
26+
{
27+
"login": "cherniavskii",
28+
"name": "Andrew Cherniavskii",
29+
"avatar_url": "https://avatars2.githubusercontent.com/u/13808724?v=4",
30+
"profile": "http://cherniavskii.com",
31+
"contributions": [
32+
"code",
33+
"bug"
34+
]
35+
}
36+
],
37+
"contributorsPerLine": 7,
38+
"skipCi": true
39+
}

README.md

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -355,21 +355,21 @@ The query `info` returned contains all information about the query and can be ea
355355
function Todos() {
356356
const { status, data, error } = useQuery('todos', fetchTodoList)
357357

358+
if (status === 'loading') {
359+
return <span>Loading...</span>
360+
}
361+
362+
if (status === 'error') {
363+
return <span>Error: {error.message}</span>
364+
}
365+
366+
// also status === 'success', but "else" logic works, too
358367
return (
359-
<div>
360-
{status === 'loading' ? (
361-
<span>Loading...</span>
362-
) : status === 'error' ? (
363-
<span>Error: {error.message}</span>
364-
) : (
365-
// also status === 'success', but "else" logic works, too
366-
<ul>
367-
{data.map(todo => (
368-
<li key={todo.id}>{todo.title}</li>
369-
))}
370-
</ul>
371-
)}
372-
</div>
368+
<ul>
369+
{data.map(todo => (
370+
<li key={todo.id}>{todo.title}</li>
371+
))}
372+
</ul>
373373
)
374374
}
375375
```
@@ -2579,3 +2579,24 @@ setConsole({
25792579
25802580
- `console: Object`
25812581
- Must implement the `log`, `warn`, and `error` methods.
2582+
2583+
2584+
## Contributors ✨
2585+
2586+
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
2587+
2588+
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
2589+
<!-- prettier-ignore-start -->
2590+
<!-- markdownlint-disable -->
2591+
<table>
2592+
<tr>
2593+
<td align="center"><a href="https://tannerlinsley.com"><img src="https://avatars0.githubusercontent.com/u/5580297?v=4" width="100px;" alt=""/><br /><sub><b>Tanner Linsley</b></sub></a><br /><a href="https://github.com/tannerlinsley/react-query/commits?author=tannerlinsley" title="Code">💻</a> <a href="#ideas-tannerlinsley" title="Ideas, Planning, & Feedback">🤔</a> <a href="#example-tannerlinsley" title="Examples">💡</a> <a href="#maintenance-tannerlinsley" title="Maintenance">🚧</a> <a href="https://github.com/tannerlinsley/react-query/pulls?q=is%3Apr+reviewed-by%3Atannerlinsley" title="Reviewed Pull Requests">👀</a></td>
2594+
<td align="center"><a href="http://cherniavskii.com"><img src="https://avatars2.githubusercontent.com/u/13808724?v=4" width="100px;" alt=""/><br /><sub><b>Andrew Cherniavskii</b></sub></a><br /><a href="https://github.com/tannerlinsley/react-query/commits?author=cherniavskii" title="Code">💻</a> <a href="https://github.com/tannerlinsley/react-query/issues?q=author%3Acherniavskii" title="Bug reports">🐛</a></td>
2595+
</tr>
2596+
</table>
2597+
2598+
<!-- markdownlint-enable -->
2599+
<!-- prettier-ignore-end -->
2600+
<!-- ALL-CONTRIBUTORS-LIST:END -->
2601+
2602+
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

examples/pagination/pages/index.js

100755100644
Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
1-
import React from 'react'
2-
import axios from 'axios'
3-
import { usePaginatedQuery } from 'react-query'
1+
import React from "react";
2+
import axios from "axios";
3+
import { usePaginatedQuery, queryCache } from "react-query";
44

55
function Todos() {
6-
const [page, setPage] = React.useState(0)
6+
const [page, setPage] = React.useState(0);
77

8-
const fetchProjects = async (key, page = 0) => {
9-
const { data } = await axios.get('/api/projects?page=' + page)
10-
return data
11-
}
8+
const fetchProjects = React.useCallback(async (key, page = 0) => {
9+
const { data } = await axios.get("/api/projects?page=" + page);
10+
return data;
11+
}, []);
1212

1313
const {
1414
status,
1515
resolvedData,
1616
latestData,
1717
error,
18-
isFetching,
19-
} = usePaginatedQuery(['projects', page], fetchProjects)
18+
isFetching
19+
} = usePaginatedQuery(["projects", page], fetchProjects, {});
20+
21+
// Prefetch the next page!
22+
React.useEffect(() => {
23+
if (latestData?.hasMore) {
24+
queryCache.prefetchQuery(["projects", page + 1], fetchProjects);
25+
}
26+
}, [latestData, fetchProjects, page]);
2027

2128
return (
2229
<div>
@@ -28,9 +35,9 @@ function Todos() {
2835
instantaneously while they are also refetched invisibly in the
2936
background.
3037
</p>
31-
{status === 'loading' ? (
38+
{status === "loading" ? (
3239
<div>Loading...</div>
33-
) : status === 'error' ? (
40+
) : status === "error" ? (
3441
<div>Error: {error.message}</div>
3542
) : (
3643
// `resolvedData` will either resolve to the latest page's data
@@ -47,7 +54,7 @@ function Todos() {
4754
disabled={page === 0}
4855
>
4956
Previous Page
50-
</button>{' '}
57+
</button>{" "}
5158
<button
5259
onClick={() =>
5360
// Here, we use `latestData` so the Next Page
@@ -61,9 +68,9 @@ function Todos() {
6168
{// Since the last page's data potentially sticks around between page requests,
6269
// we can use `isFetching` to show a background loading
6370
// indicator since our `status === 'loading'` state won't be triggered
64-
isFetching ? <span> Loading...</span> : null}{' '}
71+
isFetching ? <span> Loading...</span> : null}{" "}
6572
</div>
66-
)
73+
);
6774
}
6875

69-
export default Todos
76+
export default Todos;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"types"
3232
],
3333
"dependencies": {
34-
"@scarf/scarf": "^0.1.3",
34+
"@scarf/scarf": "^1.0.0",
3535
"ts-toolbelt": "^6.4.2"
3636
},
3737
"peerDependencies": {

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1827,10 +1827,10 @@
18271827
dependencies:
18281828
estree-walker "^1.0.1"
18291829

1830-
"@scarf/scarf@^0.1.3":
1831-
version "0.1.3"
1832-
resolved "https://registry.yarnpkg.com/@scarf/scarf/-/scarf-0.1.3.tgz#536f4fe54c613fca20f84dc7700ed3b8f1fb73cf"
1833-
integrity sha512-ftMyZO7Mi8JQh8XLBcuH3erPGmT4CU4s131nsZIDC7/PYXxaDCJtOzbz3okH0YiTAif+DSIlFhX6d7FXLOtnvA==
1830+
"@scarf/scarf@^1.0.0":
1831+
version "1.0.0"
1832+
resolved "https://registry.yarnpkg.com/@scarf/scarf/-/scarf-1.0.0.tgz#109fc9c723bac288b92b98a23d178ec21001a961"
1833+
integrity sha512-uzjTaPmtzBLHkOrx97FKJWXeb0xzx/lOrljh7OvBNFgJM7MFS8OwO3w1CTiDNjbvtHzN4m+5DzscmNN5CxM9Og==
18341834

18351835
"@sheerun/mutationobserver-shim@^0.3.2":
18361836
version "0.3.2"

0 commit comments

Comments
 (0)