Touchpad Touchpad - 1 month ago 20
React JSX Question

finding mouse target in react

I'm trying to add a simple mouse-click tracking to later on complicate it a bit more however I've sort of hit a road block, the code below console logs:

<react></react>
when I click the div and I can't understand why or how to get the div I click on to "become" the target

import React, { Component } from 'react';

class MySuperClass extends Component {

_setPosition (e) {
console.log(e.target)
}
render () {
return (
<div className='a-class' onMouseDown={() => this._setPosition(event)}>&nbsp;</div>
);
}
}

export default MySuperClass;

Answer

You have to pass the DOM event.

<div className='a-class' onMouseDown={event => this._setPosition(event)}>&nbsp;</div>

Or you can simply do this:

<div className='a-class' onMouseDown={this._setPosition}>&nbsp;</div>