-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathindex.js
49 lines (45 loc) · 1.39 KB
/
index.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
39
40
41
42
43
44
45
46
47
48
49
import { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import {
fetchSuggestion,
selectError,
selectLoading,
// Task 18: Import the `selectSuggestion()` selector from the suggestion slice
selectSuggestion,
} from './suggestion.slice';
import './suggestion.css';
export default function Suggestion() {
// Task 19: Call useSelector() with the selectSuggestion() selector
// The component needs to access the `imageUrl` and `caption` properties of the suggestion object.
const {imageUrl, caption} = useSelector(selectSuggestion);
const loading = useSelector(selectLoading);
const error = useSelector(selectError);
const dispatch = useDispatch();
useEffect(() => {
async function loadSuggestion() {
// Task 20: Dispatch the fetchSuggestion() action creator
dispatch(fetchSuggestion());
}
loadSuggestion();
}, [dispatch]);
let render;
if (loading) {
render = <h3>Loading...</h3>;
} else if (error) {
render = <h3>Sorry, we're having trouble loading the suggestion.</h3>;
} else {
// Task 21: Enable the two JSX lines below needed to display the suggestion on the page
render = (
<>
<img alt={caption} src={imageUrl} />
<p>{caption}</p>
</>
);
}
return (
<section className="suggestion-container">
<h2>Suggestion of the Day</h2>
{render}
</section>
);
}