Skip to content

Instantly share code, notes, and snippets.

@muyiwaoyeniyi
Last active February 23, 2026 18:28
Show Gist options
  • Select an option

  • Save muyiwaoyeniyi/88cbc4f9397b0b84dc3fe8d78638b52b to your computer and use it in GitHub Desktop.

Select an option

Save muyiwaoyeniyi/88cbc4f9397b0b84dc3fe8d78638b52b to your computer and use it in GitHub Desktop.
import { useState, useEffect, Fragment } from "react";
const Images = images => {
if (images.length === 0) return <div>Loading</div>;
const imageLinks = images.map(({ link }) => link);
const [imageClickCount, setImageClickCount] = useState({});
useEffect(() => {
const obj = {};
images.forEach(({ link }) => {
obj[link] = 0;
});
setImageClickCount(obj);
});
const onImageClick = link => {
imageClickCount[link] = imageClickCount[link] + 1;
};
return (
<div>
{imageLinks.map(link => (
<>
<img src={link} onClick={onImageClick} />
{imageClickCount[link] && (
<span>{`clicked ${imageClickCount[link]} times`}</span>
)}
</>
))}
</div>
);
};
export default Images;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment