class TileImages extends React.Component {
render() {
return (
<div className="layout">
<img src={airConditioning} key="air conditioning" alt="air conditioning" />,
<img src={avacado} key="avacado" alt="avacado" />,
<img src={airplane} key="airplane" alt="airplane" />,
<img src={clothes} key="new clothes" alt="new clothes" />
</div>
)
}
}
So if I want to add an "onClick" event listener to each <img> tag, how could I do that? Would I be better off using regular javascript? Also, is it better to create separate React components for each <img> since they each have their own properties and have to behave in accordance with the other images?
I'm relatively new to React and an amateur at web development, so please bear if my code and questions make no sense...
BTW- Here's the entire code file if anyone has any tips
import React from 'react'
//images
import airConditioning from './images/tiles_0.png'
import avacado from './images/tiles_1.png'
import airplane from './images/tiles_2.png'
import clothes from './images/tiles_3.png'
//CSS styling
import './Images.css'
// const imagesArray = [
// <img src={airConditioning} key="air conditioning" alt="air conditioning" />,
// <img src={avacado} key="avacado" alt="avacado" />,
// <img src={airplane} key="airplane" alt="airplane" />,
// <img src={clothes} key="new clothes" alt="new clothes" />
// ]
export class HomePage extends React.Component {
render() {
return (
<div>
<h1>Lifestyle</h1>
<br />
<TileImages />
</div>
)
}
}
class TileImages extends React.Component {
render() {
return (
<div className="layout">
<img src={airConditioning} key="air conditioning" alt="air conditioning" />,
<img src={avacado} key="avacado" alt="avacado" />,
<img src={airplane} key="airplane" alt="airplane" />,
<img src={clothes} key="new clothes" alt="new clothes" />
</div>
)
}
}
//add classes to react to onClick the image tiles using "react --patterns--"
onclick={function}. You could also add a single onclick handler to the parent div and checkevent.currentTargetto get the image clicked ononClick, but turning those images into a "dictionary" returning an Object.entries().map is going to a lot cleaner, and easier to maintain.