Skip to content

Commit 0de5c05

Browse files
committed
solution: useApi custom hook
1 parent 3804516 commit 0de5c05

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

src/components/Feed/index.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,29 @@ import "./styles.css";
66
import fetchData from "../../services/api";
77

88
export const useApi = url => {
9-
// TODO: loading, error, data loader from API
10-
// TODO: pass the url in so that we call fetchData(url)
11-
return {
12-
loading,
13-
error,
14-
data
15-
};
16-
};
17-
18-
export const Feed = () => {
199
const [loading, setLoading] = React.useState(true);
2010
const [error, setError] = React.useState(null);
2111
const [data, setData] = React.useState(null);
2212

2313
React.useEffect(() => {
24-
fetchData("messages")
14+
fetchData(url)
2515
.then(result => {
2616
setData(result);
2717
setLoading(false);
2818
})
2919
.catch(error => {
3020
setError(error);
3121
});
32-
}, []);
22+
}, [url]);
23+
return {
24+
loading,
25+
error,
26+
data
27+
};
28+
};
3329

30+
export const Feed = () => {
31+
const { loading, error, data } = useApi("messages");
3432
if (loading) {
3533
return (
3634
<div className="Feed">

0 commit comments

Comments
 (0)