Skip to content

Commit d30f776

Browse files
committed
chore: add demo example
1 parent 132f498 commit d30f776

File tree

3 files changed

+86
-10
lines changed

3 files changed

+86
-10
lines changed

example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"license": "MIT",
66
"private": true,
77
"dependencies": {
8+
"lodash": "^4.17.21",
89
"react": "link:../node_modules/react",
910
"react-dom": "^16.9.0",
1011
"react-scripts": "^3.0.1",

example/src/App.js

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,81 @@
1-
import React from 'react'
1+
import React from 'react';
2+
import axios from 'axios';
3+
import _ from 'lodash';
24

3-
import { useMyHook } from 'react-use-cancel-token'
5+
import useCancelToken from 'react-use-cancel-token';
46

57
const App = () => {
6-
const example = useMyHook()
8+
const [search, setSearch] = React.useState('');
9+
const [searching, setSearching] = React.useState(false);
10+
const [result, setResult] = React.useState('No results.');
11+
const { newCancelToken, cancelPreviousRequest, isCancel } = useCancelToken();
12+
13+
const performSearch = async (keywords = search) => {
14+
cancelPreviousRequest();
15+
setSearching(true);
16+
17+
try {
18+
const response = await axios.get(
19+
`https://api.github.com/search/repositories?q=${keywords}&sort=stars&order=desc`,
20+
{
21+
cancelToken: newCancelToken(),
22+
}
23+
);
24+
25+
if (response.status === 200) {
26+
setResult(response.data.items);
27+
}
28+
} catch (err) {
29+
if (isCancel(err)) return;
30+
setResult('An error occured!');
31+
console.error(err);
32+
}
33+
34+
setSearching(false);
35+
};
36+
37+
const delayedSearch = React.useCallback(
38+
_.debounce((value) => performSearch(value), 700),
39+
[performSearch]
40+
);
41+
42+
const handleInputChange = (event) => {
43+
const { value } = event.target;
44+
setSearch(value);
45+
delayedSearch(value);
46+
};
47+
48+
const handleCancel = () => {
49+
cancelPreviousRequest();
50+
setSearching(false);
51+
};
52+
753
return (
8-
<div>
9-
{example}
10-
</div>
11-
)
12-
}
13-
export default App
54+
<>
55+
<input type="text" placeholder="Search..." value={search} onChange={handleInputChange} />
56+
<button onClick={handleCancel} disabled={!searching}>
57+
Cancel
58+
</button>
59+
{searching && ' Searching...'}
60+
<br />
61+
<br />
62+
<div>
63+
{_.isArray(result) ? (
64+
<>
65+
<h3>Github repositories:</h3>
66+
<ul>
67+
{result.map((item, index) => (
68+
<li key={index}>
69+
<a href={item.html_url} target="_blank" rel="noopener noreferrer">{item.name}</a>
70+
</li>
71+
))}
72+
</ul>
73+
</>
74+
) : (
75+
result
76+
)}
77+
</div>
78+
</>
79+
);
80+
};
81+
export default App;

example/yarn.lock

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2154,6 +2154,13 @@ aws4@^1.8.0:
21542154
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.11.0.tgz#d61f46d83b2519250e2784daf5b09479a8b41c59"
21552155
integrity sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA==
21562156

2157+
axios@^0.21.1:
2158+
version "0.21.1"
2159+
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.1.tgz#22563481962f4d6bde9a76d516ef0e5d3c09b2b8"
2160+
integrity sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==
2161+
dependencies:
2162+
follow-redirects "^1.10.0"
2163+
21572164
axobject-query@^2.0.2:
21582165
version "2.2.0"
21592166
resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-2.2.0.tgz#943d47e10c0b704aa42275e20edf3722648989be"
@@ -4590,7 +4597,7 @@ flush-write-stream@^1.0.0:
45904597
inherits "^2.0.3"
45914598
readable-stream "^2.3.6"
45924599

4593-
follow-redirects@^1.0.0:
4600+
follow-redirects@^1.0.0, follow-redirects@^1.10.0:
45944601
version "1.14.1"
45954602
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.1.tgz#d9114ded0a1cfdd334e164e6662ad02bfd91ff43"
45964603
integrity sha512-HWqDgT7ZEkqRzBvc2s64vSZ/hfOceEol3ac/7tKwzuvEyWx3/4UegXh5oBOIotkGsObyk3xznnSRVADBgWSQVg==

0 commit comments

Comments
 (0)