I have this child component which generates list elements which contain an image and 2 icons within an a tag.
return <li><img src={bild[1]} alt="" /><span>{bild[2]}</span><a onClick={this.showBild} href="#"><i className="fa fa-eye"></i></a><a href="#"><i className="fa fa-trash-o"></i></a></li>
This return is the return of a map function of a prop that has been passed from the parent component.
Somehow the onClick event doesn't trigger my showBild function.
Did I miss something ?
Heres my whole react code including parent and child component (showBild class can be ignored)
<script type="text/jsx">
var Bilder = React.createClass({
getInitialState: function() {
return {
data:[
['1', 'http://localhost/foodoo/app/public/imgs/aichl.jpg', 'name.jpg'],
['2', 'http://localhost/foodoo/app/public/imgs/aichl.jpg', 'name.jpg'],
['3', 'http://localhost/foodoo/app/public/imgs/aichl.jpg', 'name.jpg'],
['4', 'http://localhost/foodoo/app/public/imgs/aichl.jpg', 'name.jpg'],
['5', 'http://localhost/foodoo/app/public/imgs/aichl.jpg', 'name.jpg']
],
currentPic:[],
display:'showBild clearfix dn'
};
},
bild: function(){
},
render: function() {
return (
<div>
<SingleBild data={this.state.data} chosenBild={this.bild} />
<BildDetail data={this.state.currentPic} display={this.state.display} />
</div>
);
}
});
var SingleBild = React.createClass({
getInitialState: function() {
return {
bild:[]
};
},
showBild: function(e){
e.preventDefault();
alert("yolo");
this.props.chosenBild(this.state.bild);
},
render: function() {
var displayBild = function(bild){
return <li><img src={bild[1]} alt="" /><span>{bild[2]}</span><a onClick={this.showBild} href="#"><i className="fa fa-eye"></i></a><a href="#"><i className="fa fa-trash-o"></i></a></li>
};
return (
<ul className="dashboardUl bilder clearfix">
<h2>Gemeldete Bilder:</h2>
{this.props.data.map(displayBild)}
</ul>
);
}
});
var BildDetail = React.createClass({
render: function() {
return (
<div className={this.props.display}>
<img src={this.props.data[1]} alt="" />
<ul>
<li><a href="#"><i className="fa fa-trash-o"></i></a></li>
<li><a href="#"><i className="fa fa-ban"></i></a></li>
</ul>
</div>
);
}
});
React.render(<Bilder />, document.getElementById('bilder'));
</script>
thisis probably no longer correctly in scope due to yourdisplayBuildfunction. You could make it a method on the component or bind it tothisor create an outer variable likevar component = thisand use that in place of thethisin theonClickparameter.