Parent's onClick squashed by child's onClick
See original GitHub issueChild 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:
- Created 10 years ago
- Comments:7 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Here’s a modernized version of the example given above.
That was very informative, thanks.