I have a webpage which I am building using React JS. The webpage displays a list of images in the map function within render. When I click on the image, as would be expected from my code, a border appears around every image in the map function. However, I only want a border to appear for the image that has been clicked. Any idea how I can modify my code to do that? Thanks.
import React from 'react';
import { Container, Button} from 'reactstrap';
import './scrapeInstagram.css';
const CSSVariables = {
border : {
border : '2px solid green'
},
noBorder : {
border : '2px solid transparent'
},
};
export default class ScrapeInstagram extends React.Component {
constructor(props) {
super(props);
this.state = {
showBorder : false
};
this.searchTerms = props.location.params["searchTerms"];
this.searchResults = props.location.params["searchResults"].filePaths;
this.imageClick = this.imageClick.bind(this);
}
imageClick(image_src) {
this.setState(state => ({ showBorder: !state.showBorder }))
}
render() {
return (
<Container>
<center>
<h1> IMAGES!!! </h1>
<div className="box">
{this.searchResults.map((photo) => {
return <div className="dataSetBox" key={photo.toString()}><img id={this.state.ID} src={photo} onClick={() => { this.imageClick(photo.toString()) }} style={this.state.showBorder ? CSSVariables.border : CSSVariables.noBorder} alt="Image" height="350" width="450" margin="100px" /></div>;
})}
</div>
</center>
</Container>
);
}
}
onClick((event) => {event.target.style = CSSVariables.border})