Skip to content

Commit b812631

Browse files
committed
Better Pagination support, better examples
1 parent 33f7e0a commit b812631

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+33728
-92
lines changed

.babelrc

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
[
44
"@babel/env",
55
{
6-
"modules": false
6+
"modules": false,
7+
"exclude": ["@babel/plugin-transform-regenerator"]
78
}
89
],
910
"@babel/react"
10-
]
11+
],
12+
"plugins": ["module:fast-async"]
1113
}

README.md

+186-46
Large diffs are not rendered by default.

examples/api-hooks/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Example
2+
3+
To run this example:
4+
5+
- `npm install` or `yarn`
6+
- `npm run dev` or `yarn dev`
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { useQuery } from 'react-query'
2+
3+
import fetch from '../libs/fetch'
4+
5+
function useProjects() {
6+
return useQuery('projects', () => fetch('/api/data'))
7+
}
8+
9+
export default useProjects
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { useQuery } from 'react-query'
2+
3+
import fetch from '../libs/fetch'
4+
5+
function useRepository(id) {
6+
return useQuery(['repository', { id }], () => fetch('/api/data?id=' + id))
7+
}
8+
9+
export default useRepository

examples/api-hooks/libs/fetch.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import fetch from 'isomorphic-unfetch'
2+
3+
export default async function(...args) {
4+
const res = await fetch(...args)
5+
return await res.json()
6+
}

examples/api-hooks/package.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "basic",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"dependencies": {
7+
"isomorphic-unfetch": "3.0.0",
8+
"next": "9.1.2",
9+
"react-query": "latest"
10+
},
11+
"scripts": {
12+
"dev": "next",
13+
"start": "next start",
14+
"build": "next build"
15+
}
16+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React from 'react'
2+
import Link from 'next/link'
3+
import useRepository from '../../hooks/use-repository'
4+
5+
export default () => {
6+
const id =
7+
typeof window !== 'undefined' ? window.location.pathname.slice(1) : ''
8+
const { data, isLoading, isFetching } = useRepository(id)
9+
10+
return (
11+
<div style={{ textAlign: 'center' }}>
12+
<h1>{id}</h1>
13+
{isLoading ? (
14+
'Loading...'
15+
) : data ? (
16+
<>
17+
<div>
18+
<p>forks: {data.forks_count}</p>
19+
<p>stars: {data.stargazers_count}</p>
20+
<p>watchers: {data.watchers}</p>
21+
</div>
22+
<div>{isFetching ? 'Background Updating...' : ' '}</div>
23+
</>
24+
) : null}
25+
<br />
26+
<br />
27+
<Link href="/">
28+
<a>Back</a>
29+
</Link>
30+
</div>
31+
)
32+
}

examples/api-hooks/pages/api/data.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import fetch from 'isomorphic-unfetch'
2+
3+
const projects = [
4+
'facebook/flipper',
5+
'vuejs/vuepress',
6+
'rust-lang/rust',
7+
'zeit/next.js',
8+
]
9+
10+
export default (req, res) => {
11+
if (req.query.id) {
12+
// a slow endpoint for getting repo data
13+
fetch(`https://api.github.com/repos/${req.query.id}`)
14+
.then(resp => resp.json())
15+
.then(data => {
16+
setTimeout(() => {
17+
res.json(data)
18+
}, 2000)
19+
})
20+
21+
return
22+
}
23+
setTimeout(() => {
24+
res.json(projects)
25+
}, 2000)
26+
}

examples/api-hooks/pages/index.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from 'react'
2+
import Link from 'next/link'
3+
import useProjects from '../hooks/use-projects'
4+
5+
export default () => {
6+
const { data, isLoading, isFetching } = useProjects()
7+
8+
return (
9+
<div style={{ textAlign: 'center' }}>
10+
<h1>Trending Projects</h1>
11+
<div>
12+
{isLoading ? (
13+
'Loading...'
14+
) : data ? (
15+
<>
16+
<div>
17+
{data.map(project => (
18+
<p key={project}>
19+
<Link href="/[user]/[repo]" as={`/${project}`}>
20+
<a>{project}</a>
21+
</Link>
22+
</p>
23+
))}
24+
</div>
25+
<div>{isFetching ? 'Background Updating...' : ' '}</div>
26+
</>
27+
) : null}
28+
</div>
29+
</div>
30+
)
31+
}

0 commit comments

Comments
 (0)