Set an onClick listener on an Image in React

avatar

Borislav Hadzhiev

Last updated: Apr 26, 2022

banner

Photo from Unsplash

Set an onClick listener on an Image in React #

To set an onClick listener on an image in React:

  1. Set the onClick prop on the image element.
  2. The function you pass to the prop will get called every time the image is clicked.
App.js
const App = () => { const handleClick = event => { // 👇️ refers to the image element console.log(event.target); console.log('Image clicked'); }; return ( <div> <img src="https://bobbyhadz.com/images/blog/react-prevent-multiple-button-clicks/thumbnail.webp" alt="car" onClick={handleClick} /> </div> ); }; export default App;

We set the onClick prop on the image element, so every time the element is clicked, the handleClick function gets invoked.

If you need to access the image element in your handleClick function, access the target property on the event object.

If the event is not being triggered, make sure your image element does not have the CSS property pointer-events set to none.

If you want to pass a parameter to the handleClick function, set the onClick prop to an inline arrow function.

App.js
const App = () => { const handleClick = (event, message) => { // 👇️ refers to the image element console.log(event.target); console.log(message); console.log('Image clicked'); }; return ( <div> <img src="https://bobbyhadz.com/images/blog/react-prevent-multiple-button-clicks/thumbnail.webp" alt="car" onClick={(event) => handleClick(event, 'hello')} /> </div> ); }; export default App;

Notice that we are passing a function to the onClick prop and not the result of calling one.

If you invoke the function when passing it to the onClick prop, e.g. onClick={handleClick()}, it would get immediately called when the component mounts.

You can use the same approach to set the onClick prop on a local image.

App.js
import MyImage from './thumbnail.webp'; const App = () => { const handleClick = event => { // 👇️ refers to the image element console.log(event.target); console.log('Image clicked'); }; return ( <div> <img src={MyImage} alt="horse" onClick={handleClick} /> </div> ); }; export default App;

The example assumes that you have an image named thumbnail.webp in the same folder as the App component.

Make sure to specify the correct path to the image file (including the extension).

For example, if you are importing an image from one directory up, you would import as import MyImage from '../thumbnail.webp'.

The image has to be located in the src directory of your project.

Notice that in both examples we set the src prop on the img tag in order to render the image.

With images from an external URL, you set the src prop of the image to the complete URL.

With local images, you set the src proper of the img tag to the imported image.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.