I'm trying to use D3.js event listeners with arrow functions, but it does not seem to work.
this is bind to undefined.
How can I access this using ES6 arrow functions?
ES5:
svg.selectAll('circle')
.data(data)
.enter()
.append('circle')
.attr('r', '10')
.attr('cx', (d) => x(d.day))
.attr('cy', (d) => y(d.amount))
.on('mouseenter', function (d) {
console.log(this); // output '<circle r="10" cx="10" cy="1"/>'
});
ES6 (using arrow function):
svg.selectAll('circle')
.data(data)
.enter()
.append('circle')
.attr('r', '10')
.attr('cx', (d) => x(d.day))
.attr('cy', (d) => y(d.amount))
.on('mouseenter', (d) => {
console.log(this); // output: 'undefined'
});