I have a component which displays images on click of a button. These images when get displayed on the screen, I want to apply an onClick listener to them such that the image that I click on gets displayed on the whole screen.
Code:
class App extends React.Component{
constructor(props){
super(props);
this.state={
images:[],
pano:'',
name:'',
list:[]
}
this.loadImages=this.loadImages.bind(this);
this.loadOne=this.loadOne.bind(this);
}
loadImages(){
console.log("load");
var that=this;
$.ajax({
type:'GET',
url:'https://demo0813639.mockable.io/getPanos',
success:function(result){
var images=that.state.images;
for(var i=0;i<result.length;i++){
that.state.images.push({"pano":result[i].pano,"name":result[i].name});
}
that.setState({
images:images
})
}
})
}
loadOne(url){
console.log("hi")
}
render(){
var list=this.state.list;
list=this.state.images.map(function(result){
//console.log(result.name);
return(<div className="box">
<div className="label">{result.name}</div>
<img src={result.pano} className="image"/>
</div>
)
})
return(
<div>
<button onClick={this.loadImages}>Click</button>
<div onClick={this.loadOne(this,this.props.result.pano)}>{list}</div>
</div>
);
}
}
loadOne()
is the function which gets called after clicking on an image. I get the error:
cannot read property pano of undefined
And if I do like this:
render(){
var list=this.state.list;
list=this.state.images.map(function(result){
return(<div className="box">
<div className="label">{result.name}</div>
<img src={result.pano} className="image" onClick={this.loadOne(this,this.props.result.pano)}/>
</div>
)
})
return(
<div>
<button onClick={this.loadImages}>Click</button>
<div >{list}</div>
</div>
);
}
then I get the error:
cannot read property loadOne of undefined.
So, how can I pass specific image url to my function?