1import React, { useState } from "react";
2import "./Styles/_imagegallery.scss";
3
4export default function ImageGallery({ ...props }) {
5 const { children } = props;
6
7 const firstChild = children[0].props;
8
9 const [img, setImg] = useState({ src: firstChild.src, alt: firstChild.alt });
10
11 const onImageChange = (props) => {
12 const { src, alt } = props;
13 setImg({ ...img, src, alt });
14 };
15
16 const childrenWithProps = React.Children.map(children, (child) => {
17 if (React.isValidElement(child)) {
18 return React.cloneElement(child, { onImageChange, img });
19 }
20 return child;
21 });
22
23 return (
24 <div className="Image-Gallery">
25 <div>
26 <img src={img.src} alt={img.alt} />
27 </div>
28 <div>{childrenWithProps}</div>
29 </div>
30 );
31}
32
33export { ImageGalleryItem } from "./Components/ImageGalleryItem";