Skip to content

Commit 83134cb

Browse files
authored
Merge pull request #3 from the-road-to-learn-react/fix-use-effect-example
Fix use effect example
2 parents 788a53c + 19da714 commit 83134cb

File tree

3 files changed

+76
-37
lines changed

3 files changed

+76
-37
lines changed

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import React from 'react';
22
import ReactDOM from 'react-dom';
33

44
// import App from './useState-example';
5-
// import App from './useEffect-example';
5+
import App from './useEffect-example';
66
// import App from './customHook-example';
7-
import App from './useDataApiHook-example';
7+
// import App from './useDataApiHook-example';
88

99
import * as serviceWorker from './serviceWorker';
1010

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

src/useEffect-example/index.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@ function App() {
44
const [isOn, setIsOn] = useState(false);
55
const [timer, setTimer] = useState(0);
66

7-
useEffect(() => {
8-
const interval = setInterval(
9-
() => isOn && setTimer(timer + 1),
10-
1000,
11-
);
12-
13-
return () => clearInterval(interval);
14-
}, []);
7+
useEffect(
8+
() => {
9+
let interval;
10+
11+
if (isOn) {
12+
interval = setInterval(
13+
() => setTimer(timer => timer + 1),
14+
1000,
15+
);
16+
}
17+
18+
return () => clearInterval(interval);
19+
},
20+
[isOn],
21+
);
1522

1623
const onReset = () => {
1724
setIsOn(false);

0 commit comments

Comments
 (0)