question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parent's onClick squashed by child's onClick

See original GitHub issue

Child BigButton’s render:

return this.transferPropsTo(<button onClick={this.childHandleClick}>Press</button>);

Parent’s render:

return (<BigButton onClick={this.parentHandleClick} /><h1>hello</h1>);

In this case, childHandleClick will apparently squash parentHandleClick, and unless childHandleClick calls this.props.onClick(); inside its body there’s no way to trigger parent’s click event. This kind of sucks, because each time the child uses a similar event it needs to do a conditional to check if the parent’s also employing it.

Also, somewhat related: is it better to attach an event like done above or to do it in componentDidMount? Waiting for 4.0’s best practices.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
rafaeldwancommented, Aug 8, 2018

Here’s a modernized version of the example given above.

class BigButton extends React.Component {
  childHandleClick = (e) => {
    alert('big button clicked!')
    this.props.onBigButtonClick()
  }
  render() {
    return (
      <button onClick={this.childHandleClick}>
        <span />
      </button>
    )
  }
}

class Parent extends React.Component {
  parentHandleClick = () => {
    alert('parent clicked')
  }
  render() {
    return (
      <BigButton onBigButtonClick={this.parentHandleClick} />
    )
  }
}
0reactions
chengloucommented, Jul 6, 2013

That was very informative, thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I prevent a parent's onclick event from firing when a ...
By a adding an onClick function for the child modal (content div) mouse click events are prevented to reach the 'closeLogin' function of...
Read more >
Capture only parent's onClick event in React | bobbyhadz
To capture only parent's onClick event in React, add an onClick event handler to the parent element. Check if `event.target` is equal to...
Read more >
[Solved]-How to add an onClick for a parent div and not letting ...
Coding example for the question How to add an onClick for a parent div and not letting its children inherit it?-Reactjs.
Read more >
Beyond onClick: Handling Events in React | Lightstep Blog
Functions for handling events are often defined in parents and passed to children before being set as an event handler prop.
Read more >
prevent parent click event when child is clicked - Code Grepper
how can i stop an onclick event from firing for parent element when child is clicked? how to prevent parent element to listen...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found