【发布时间】:2018-03-08 17:50:50
【问题描述】:
我有一个使用 React JS 构建的网页。该网页在render 内的map 函数中显示图像列表。当我单击图像时,正如我的代码所预期的那样,map 函数中的每个图像周围都会出现一个边框。但是,我只想为已单击的图像显示一个边框。知道如何修改我的代码来做到这一点吗?谢谢。
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}) -
@EgorEgorov 感谢您的回复 - 不幸的是,我无法让它工作。我没有收到任何错误,但没有显示边框
标签: javascript reactjs onclick