-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEpisodeListFetched.js
38 lines (27 loc) · 1.17 KB
/
EpisodeListFetched.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import React, { useState, useEffect } from "react";
import EpisodeListSearchable from './EpisodeListSearchable';
function EpisodeListFetched(props) {
const showId = props.showId;
//STATE HOOK: the fetched episodes - initially none
const [episodes, setEpisodes] = useState([]);
//STATE HOOK: boolean - is the data loading or not?
const [loading, setLoading] = useState(false);
//EFFECT HOOK: Fetch episodes from API on component mount, only.
useEffect(() => {
fetchEpisodesForShow(showId)
}, [showId]); //Note: IMPORTANT don't forget [] as last param so
// that useEffect does not execute on any state change
// (INCLUDING the one it instigates)
async function fetchEpisodesForShow(showId) {
setLoading(true);
const result = await fetch(`https://api.tvmaze.com/shows/${showId}/episodes`);
//Note: we should handle errors here
const json = await result.json();
setEpisodes(json);
setLoading(false);
};
return loading ?
<div className="loading" > Loading episodes...</div>
: <EpisodeListSearchable episodes={episodes} />;
}
export default EpisodeListFetched;