Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fd86bb4

Browse files
committed
finish fetch data example
1 parent 8beb36a commit fd86bb4

File tree

1 file changed

+59
-27
lines changed

1 file changed

+59
-27
lines changed

‎src/useDataApiHook-example/index.js

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,75 @@
1-
import React, { useState, useEffect } from 'react';
1+
import React, { Fragment,useState, useEffect } from 'react';
22
import axios from 'axios';
33

4-
const useDataApi = url => {
5-
const [isLoading, setLoading] = useState(false);
6-
const [data, setData] = useState(null);
4+
const useDataApi = (url, defaultSearch, defaultState) => {
5+
const [data, setData] = useState(defaultState);
6+
const [search, setSearch] = useState(defaultSearch);
7+
const [isLoading, setIsLoading] = useState(false);
8+
const [isError, setIsError] = useState(false);
79

8-
useEffect(async () => {
9-
setLoading(true);
10+
useEffect(
11+
async () => {
12+
setIsError(false);
13+
setIsLoading(true);
1014

11-
const result = await axios(url);
15+
try {
16+
const result = await axios(`${url}${search}`);
1217

13-
setLoading(false);
14-
setData(result.data);
15-
}, []);
18+
setData(result.data);
19+
} catch (error) {
20+
setIsError(true);
21+
}
1622

17-
return { data, isLoading };
23+
setIsLoading(false);
24+
},
25+
[search],
26+
);
27+
28+
const doSearch = (event, query) => {
29+
setSearch(query);
30+
event.preventDefault();
31+
};
32+
33+
return { data, isLoading, isError, doSearch };
1834
};
1935

36+
const INITIAL_SEARCH = 'redux';
37+
const INITIAL_DATA = { hits: [] };
38+
2039
function App() {
21-
const { data, isLoading } = useDataApi(
22-
'http://hn.algolia.com/api/v1/search?query=redux',
40+
const [query, setQuery] = useState(INITIAL_SEARCH);
41+
42+
const { data, isLoading, isError, doSearch } = useDataApi(
43+
'http://hn.algolia.com/api/v1/search?query=',
44+
INITIAL_SEARCH,
45+
INITIAL_DATA,
2346
);
2447

25-
if (isLoading) {
26-
return <div>Loading ...</div>;
27-
}
48+
return (
49+
<Fragment>
50+
<form onSubmit={event => doSearch(event, query)}>
51+
<input
52+
type="text"
53+
value={query}
54+
onChange={event => setQuery(event.target.value)}
55+
/>
56+
<button type="submit">Search</button>
57+
</form>
2858

29-
if (!data) {
30-
return null;
31-
}
59+
{isError && <div>Something went wrong ...</div>}
3260

33-
return (
34-
<ul>
35-
{data.hits.map(item => (
36-
<li key={item.objectID}>
37-
<a href={item.url}>{item.title}</a>
38-
</li>
39-
))}
40-
</ul>
61+
{isLoading ? (
62+
<div>Loading ...</div>
63+
) : (
64+
<ul>
65+
{data.hits.map(item => (
66+
<li key={item.objectID}>
67+
<a href={item.url}>{item.title}</a>
68+
</li>
69+
))}
70+
</ul>
71+
)}
72+
</Fragment>
4173
);
4274
}
4375

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /