I have just started to work with ReactJS and I can't figure out how I can make onClick events work inside a map function, which renders a few similar elements. When I am not using onClick={this.someMethodName} in the child elements, everything is working fine, so I am guessing the 'this' keyword no longer refers to the parent.
Here is my code, which renders a simple menubar:
var SupplierBarComponent = React.createClass({
doSomething: function () {
console.log("aaaaaaaah");
},
render: function() {
const suppliers = this.props.data.map(function(supplier){
return (
<option onClick={this.doSomething} key={supplier.id}>{supplier.name}</option>
);
}, this);
return(
<div>{suppliers}</div>
);
}
How can I make it work? I have read a few similar questions*, but their solutions didn't work for me, or I couldn't make them work.
*: React onClick inside .map then change data in another sibling component "this" is undefined inside map function Reactjs Pass props to parent component in React.js