function ListPhotos() {
const [photos, setPhotos] = useState([])
const [perPage, setPerpage] = useState(1)
const [hovered, setHovered] = useState(false)
const getData = useCallback(() => {
unsplash.photos
.listPhotos(perPage, 15, "latest")
.then(toJson)
.then(json => {
setPhotos([...photos, ...json])
setPerpage(perPage + 1)
})
}, [photos, perPage])
useEffect(() => {
getData()
}, [])
const styles = {
padding: "15px",
}
const childElements = photos.map(photo => (
<li className="list-unstyled">
<Link to={`/${photo.id}`}>
<img
className="rounded"
style={styles}
src={photo.urls.small}
alt={photo.description}
onMouseOut={() => {
setHovered(true)
}}
onMouseOver={() => {
setHovered(false)
}}
/>
</Link>
</li>
))
return (
<div className="container-fluid">
<div className="row p-4">
<div className="col-12">
<InfiniteScroll dataLength={photos.length}
next={getData}
hasMore={true}
loader={<div className="spinner-border" role="status">
<span className="sr-only">Loading...</span>
</div>}
>
<Masonry elementType={"ul"}>
{childElements}
</Masonry>
</InfiniteScroll>
</div>
</div>
</div>
)
}
export default ListPhotos
const childElements = photos.map(photo => (
<li className="list-unstyled">
<Link to={`/${photo.id}`}>
<img
className={`rounded ${ hovered === photo.id ? 'hover-class' : '' }`}
style={styles}
src={photo.urls.small}
alt={photo.description}
onMouseOut={() => {
setHovered(null)
}}
onMouseOver={() => {
setHovered(photo.id)
}}
/>
</Link>
</li>
))