I have a simple sidebar, that has a bunch of list items in it, and a button right next to the list item, like so:
I attached a click handler to the <li>
element like in the code below:
<li className="note" onClick={()=> props.selectNote(props.note)} role="button">
<button className="delete-note" onClick={() => console.log('Fired')}>Delete dis</button>
<span className="updated-at">2hr</span>
<div className="note-content">
<h4 className="note-title">
{title}
</h4>
<p className="note-preview">
{notePreview.substr(0, 80)}...
</p>
</div>
</li>
But as expected, when I click the button next to it, the actual li
gets clicked and not the button inside it. I think this is some sort of issue with how the event bubbles, and how it's a bad practice to attach onClick
handlers to non-interactive elements (My ESLint says that).
What I want instead:
- When the list item gets clicked, the attached
onClick
event fire. - When the button gets clicked, fire the
onClick
event on the button, and not the<li>
.