Created
February 27, 2026 19:11
-
-
Save muyiwaoyeniyi/183dbef1b724d79720b2d0e0100b3a12 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { useState, useEffect, Fragment } from "react"; | |
| const Images = images => { | |
| if (images === undefined) return <div>Loading</div>; | |
| const imageLinks = images.map(({ link }) => link); | |
| const [imageClickCount, setImageClickCount] = useState({}); | |
| const buildImageClickCount = () => { | |
| const obj = {}; | |
| images.forEach(({ link }) => { | |
| obj[link] = imageClickCount[link] || 0; | |
| }); | |
| return obj; | |
| }; | |
| useEffect(() => { | |
| setImageClickCount(buildImageClickCount()); | |
| }); | |
| 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