The key thing to note is the difference between normal function expressions and the new ES6 arrow functions. In short: unlike function expressions arrow functions do not maintain their own this
(or arguments
) - they borrow it from the outer lexical environment.
"An arrow function expression is a syntactically compact alternative to a regular function expression, although without its own bindings to the this, arguments, super, or new.target keywords. Arrow function expressions are ill suited as methods, and they cannot be used as constructors."
So when you refer to this
in that map
callback this
doesn't refer to the class.
So 1) change the function expression to an arrow function:
let result = items.map((value, i) => {
return (
<List.Item key={i}>
<a onClick={this.getFacilities}>{value.user.name}</a>
</List.Item>
);
});
2) Since you're using an arrow function for your class method you can remove as this would only be required if you were using a standard class method definition rather than a class field.
this.getFacilities = this.getFacilities.bind(this);
However there's some evidence to suggest that class fields (using arrow functions) such as these are worse than using standard class method format, so you may be better off keeping the binding in the constructor and using the standard format for functions that you're passing around:
getFacilities(e) {
console.log(e)
}
It's something to bear in mind.
PS, from that link:
"It is unnecessary to do that to every function. This is just as bad as autobinding (on a class). You only need to bind functions that you pass around. e.g. onClick={this.doSomething}
. Or fetch.then(this.handleDone)
— Dan Abramov"