5

When I click on the div that includes the AnimatedMouse component the event that logged in the console is one of the elements of AnimatedMouse component not the div with className "animated-mouse" itself.

const AnimatedMouseScroll = () => { return ( <div class="scroll-msg-container"> <div class="scroll-msg-inner"> <div class="scroll-msg-wheel"> click here </div> </div> </div> ); } const EmployerService = () => { const scrollToNextSectionHandler = (event) => { console.log("clicked element", event.target) }; return ( <div className="animated-mouse" onClick={(e) => scrollToNextSectionHandler(e)}> <AnimatedMouseScroll /> </div> ); } ReactDOM.render(<EmployerService />, document.getElementById("root"));
<div id="root"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.10.2/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.10.2/umd/react-dom.production.min.js"></script>

4
  • You haven't included AnimatedMouse. You've included AnimatedMouseScroll, but not AnimatedMouse. I've turned your code blocks into a Stack Snippet (the [<>] toolbar button) and changed AnimatedMouse to AnimatedMouseScroll and it seems to show what you're trying to show. For next time, here's how to do one. Runnable examples help you ensure you've included everything necessary, and make it much easier for people to help you. Commented Jan 20, 2020 at 7:47
  • try onClick={(e)=> {e.stopPropagation(); scrollToNextSectionHandler(e);}} Commented Jan 20, 2020 at 7:47
  • @ArshpreetWadehra - That won't make any difference at all. Commented Jan 20, 2020 at 7:49
  • @T.J.Crowder I made a mistake in copy and paste the code, but in the real project, everything is ok. Commented Jan 20, 2020 at 7:52

1 Answer 1

5

target is always the innermost target element that was clicked. (This is true in the DOM, not just React.) If you want the element you've put the event handler on, use currentTarget:

console.log("clicked element", event.currentTarget ) // −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−^ 

Live Example:

const AnimatedMouseScroll = () => { return ( <div class="scroll-msg-container"> <div class="scroll-msg-inner" > <div class= "scroll-msg-wheel" > click here </div> </div> </div> ); } const EmployerService = () => { const scrollToNextSectionHandler = (event) => { console.log("clicked element", event.currentTarget ) }; return ( <div className="animated-mouse" onClick={(e) => scrollToNextSectionHandler(e)}> <AnimatedMouseScroll /> </div> ); } ReactDOM.render(<EmployerService/>, document.getElementById("root"));
<div id="root"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.10.2/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.10.2/umd/react-dom.production.min.js"></script>

Sign up to request clarification or add additional context in comments.

1 Comment

@s.hesam - No worries!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.