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.

Is there a better alternative to cloneElement?

See original GitHub issue

When using ToggleButton from lab package and wrapping it in tooltip it raises a warning: Warning: Failed prop type: The following properties are not supported: selected, onChange. Please remove them.

  • This is not a v1.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

It should be possible to add a tooltip to ToggleButton

Current Behavior

<Tooltip title="left" placement="top">
  <ToggleButton value="left">
    <FormatAlignLeftIcon />
  </ToggleButton>
</Tooltip>

Steps to Reproduce

Link: https://codesandbox.io/s/q7m82k4024

  1. Wrap the ToggleButton inside a Tooltip
  2. Run the code
  3. See the console for warning

Context

Since ToggleButtons are IconButton (at least that’s how we intend to use it) it would be helpful to a user to see what that button will do and the best way for that is Tooltip

Your Environment

Tech Version
Material-UI v3.1.0
React v16.4
Browser any

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:13
  • Comments:23 (12 by maintainers)

github_iconTop GitHub Comments

3reactions
totszwaicommented, Mar 28, 2022

Guys, Tooltip breaks ToggleButton.

The selected CSS class will not get set if you wraps the Tooltip around the button.

<ToggleButtonGroup>
  <Tooltip title="whatever1">
     <ToggleButton value="blah1">Blah1</ToggleButton>
  </Tooltip>
  <Tooltip title="whatever2">
     <ToggleButton value="blah2">Blah2</ToggleButton>
  </Tooltip>
</ToogleButtonGroup>

Without Tooltip you can see that the proper class is being added: image

When wrapping the ToggleButton with a tooltip, it breaks: image

3reactions
eps1loncommented, Sep 19, 2018

Just a random thought but wouldn’t it be better to create a context in ToggleButtonGroup that the ToggleButtons can listen to? That way we could get away from the cloneElement pattern.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a better alternative to cloneElement? #12921 - GitHub
This component assumes it's immediate children will be buttons and passes 'selected' and 'onChange' props. Now, this can be solved if Tooltip ......
Read more >
When should I be using React.cloneElement vs this.props ...
Look at Vennesa's answer instead, which is a better explanation. Original: First of all, the React.cloneElement example only works if your child ...
Read more >
react: spreading children vs cloneelement - code-comments
Instead of lifting the state, there's an alternative approach - but it means no longer simply spreading the children props as in the...
Read more >
Advanced composition in React: cloneElement, HOCs and ...
It's similar to using cloneElement , but you give the component passing the ... This solution is more flexible than the previous ones....
Read more >
cloneElement - React Docs
Using cloneElement is uncommon and can lead to fragile code. See common alternatives. cloneElement lets you create a new React element using another...
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