简体   繁体   中英

ReactJS: Issue with accessing event.target children

I have within my React App a component containing an element with an onMouseEnter/Leave handler

<CollapseElement>
  <li onMouseEnter={e => handleEnter(e)}>
   {props.children}
  </li>
</CollapseElement>

The li is passed children through props, amongst which an element with a className of "targetelement" that I would like to access within the handleEnter of CollpaseElement component and pass it to a function

const handleEnter = (e) => {
        e.preventDefault();
        console.log(e.currentTarget.innerHTML)
        function(...)
    }

While the console.log does show me the innerHTML amongst which the target, I cannot figure a way of selecting it. I have tried querySelector('.targetelement'), closest or querySelectorAll to no success: either a null error or, for example if the function is trying to get the coordinates of the target element, I end up with all values at 0

Thank you for your input

Solved. The issue was in understanding the sync and rendering of React/DOM elements. By using useEffect in the parent element with the correct event sent via the context, I am able to target the child with the class I needed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM