[ClickAwayListemer] React.cloneElement can lead to overriding children onClick event
See original GitHub issue- The issue is present in the latest release.
- I have searched the issues of this repository and believe that this is not a duplicate.
Current Behavior 😯
Imagine you have a ClickAwayListener
component which has a Grow
transition as the child. And, this transition, in turn, has an element with props that ClickAwayListener
would use, like onClick
handlers. Those props are not forwarded by the Grow
element.
Expected Behavior 🤔
The props are forwarded.
Steps to Reproduce 🕹
Open this sandbox and take a good look at the code. See that we’re using the Grow
transition to create a nice animation when the Popper opens. Click on the button and click inside the Paper element. It should do nothing.
Now, comment out the Grow
element and try clicking again inside the Poper element: you’ll get the alert saying Hi
.
Your Environment 🌎
Tech | Version |
---|---|
Material-UI | v4.11.0 |
React | 16.12.0 |
Browser | Chrome 84.0.4147.105 |
TypeScript | 3.9.3 |
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
[ClickAwayListemer] React.cloneElement can lead to ... - GitHub
Current Behavior Imagine you have a ClickAwayListener component ... cloneElement can lead to overriding children onClick event #22156.
Read more >How to adjust the onClick event of react.children when child is ...
When creating a component that uses the children prop I would like to adjust its onclick event. This is easy if the child...
Read more >Using the React.cloneElement() function to clone elements
In this in-depth tutorial, learn how to clone and manipulate elements three different ways using the React.cloneElement function.
Read more >cloneElement - React Docs
Using cloneElement is uncommon and can lead to fragile code. ... To override the props of some React element, pass it to cloneElement...
Read more >material-ui/core/CHANGELOG.md - UNPKG
The best support for React 17 will be found in Material-UI v5. ... 324, - [docs] Document the onClick handler on Button (#21234)...
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 FreeTop 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
Top GitHub Comments
cloneElement
is just the only tool we have unless we add an extra<div>
on which we listen foronClick
in theClickAwayListener
. Ideally we’d have something like<React.Fragment onClick={handleClick}>{clickAwayListenerChilren}</React.Fragment>
but no similar API exists in stable React releases right now.This particular example can be fixed with
But this only works for
Grow
. Other transition components do have extra wrapper<div>
where this wouldn’t be equivalent.At least I don’t see anything actionable right now. I suspect by the point React provides better primitives we will have forgotten about this issue.
See https://github.com/mui-org/material-ui/issues/22156#issuecomment-673691101 for the current workaround.