forked from btholt/complete-intro-to-react-v5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarousel.js
More file actions
44 lines (41 loc) · 1.07 KB
/
Carousel.js
File metadata and controls
44 lines (41 loc) · 1.07 KB
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
import React from "react";
class Carousel extends React.Component {
state = {
photos: [],
active: 0
};
static getDerivedStateFromProps({ media }) {
let photos = [];
if (media && media.photos && media.photos.photo) {
photos = media.photos.photo.filter(photo => photo["@size"] === "pn");
}
return { photos };
}
handleIndexClick = event => {
this.setState({
active: +event.target.dataset.index
});
};
render() {
const { photos, active } = this.state;
return (
<div className="carousel">
<img src={photos[active].value} alt="animal" />
<div className="carousel-smaller">
{photos.map((photo, index) => (
// eslint-disable-next-line
<img
key={photo.value}
onClick={this.handleIndexClick}
data-index={index}
src={photo.value}
className={index === active ? "active" : ""}
alt="animal thumbnail"
/>
))}
</div>
</div>
);
}
}
export default Carousel;