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.

[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:closed
  • Created 3 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
eps1loncommented, Aug 13, 2020

cloneElement is just the only tool we have unless we add an extra <div> on which we listen for onClick in the ClickAwayListener. 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

-<Grow in={isOpen} appear>
+<Grow in={isOpen} appear onClick={() => alert('hi')}>
  <Paper
-   onClick={() => alert('hi')}
    elevation={2}
  >

But this only works for Grow. Other transition components do have extra wrapper <div> where this wouldn’t be equivalent.

0reactions
eps1loncommented, Aug 14, 2020

@eps1lon Should we close the issue?

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.

Read more comments on GitHub >

github_iconTop 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 >

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