Skip to content

Commit 12ae77e

Browse files
committed
add example with library
1 parent 33e54ca commit 12ae77e

File tree

5 files changed

+106
-2
lines changed

5 files changed

+106
-2
lines changed

package-lock.json

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"axios": "^0.18.0",
77
"react": "^16.8.6",
88
"react-dom": "^16.8.6",
9-
"react-scripts": "^3.0.1"
9+
"react-scripts": "^3.0.1",
10+
"use-data-api": "^1.0.0"
1011
},
1112
"scripts": {
1213
"start": "react-scripts start",

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import ReactDOM from 'react-dom';
44
// import App from './useState-example';
55
// import App from './useEffect-example';
66
// import App from './customHook-example';
7-
import App from './useDataApiHook-example';
7+
// import App from './useDataApiHook-example';
8+
import App from './useDataApiHook-external-example';
89

910
import * as serviceWorker from './serviceWorker';
1011

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React, { Fragment, useState } from 'react';
2+
3+
import useDataApi from 'use-data-api';
4+
5+
function App() {
6+
const [query, setQuery] = useState('redux');
7+
const [{ data, isLoading, isError }, doFetch] = useDataApi(
8+
'http://hn.algolia.com/api/v1/search?query=redux',
9+
{ hits: [] },
10+
);
11+
12+
return (
13+
<Fragment>
14+
<form
15+
onSubmit={event => {
16+
doFetch(
17+
`http://hn.algolia.com/api/v1/search?query=${query}`,
18+
);
19+
20+
event.preventDefault();
21+
}}
22+
>
23+
<input
24+
type="text"
25+
value={query}
26+
onChange={event => setQuery(event.target.value)}
27+
/>
28+
<button type="submit">Search</button>
29+
</form>
30+
31+
{isError && <div>Something went wrong ...</div>}
32+
33+
{isLoading ? (
34+
<div>Loading ...</div>
35+
) : (
36+
<ul>
37+
{data.hits.map(item => (
38+
<li key={item.objectID}>
39+
<a href={item.url}>{item.title}</a>
40+
</li>
41+
))}
42+
</ul>
43+
)}
44+
</Fragment>
45+
);
46+
}
47+
48+
export default App;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import App from './';
4+
5+
it('renders without crashing', () => {
6+
const div = document.createElement('div');
7+
ReactDOM.render(<App />, div);
8+
ReactDOM.unmountComponentAtNode(div);
9+
});

0 commit comments

Comments
 (0)