javascript - Why does custom Button component using MUI Button not work on hover with Tooltip? - Stack Overflow

I created a Custom Button that wraps a MUI Button in order to select default props and such. Below is a

I created a Custom Button that wraps a MUI Button in order to select default props and such. Below is a simplified example of what I created, but the issue still exists.

Below is the code and here is a codesandbox link: =/Button.js

const Button = React.forwardRef((props, ref) => (
  <MuiButton ref={ref}>{props.children}</MuiButton>
));
<Tooltip title="I can hover" placement="top">
  <Button>Custom Button</Button>
</Tooltip>

According to the Tooltip docs at , I should make sure that the child can hold a ref, so I have wrapped it in the React.forwardRef function and passed the ref to the Material UI Button ponent that it is returning, but the tooltip still isn't working.

Any ideas what I'm missing?

I created a Custom Button that wraps a MUI Button in order to select default props and such. Below is a simplified example of what I created, but the issue still exists.

Below is the code and here is a codesandbox link: https://codesandbox.io/s/material-ui-button-forwardref-tooltip-hover-problem-entv2?file=/Button.js

const Button = React.forwardRef((props, ref) => (
  <MuiButton ref={ref}>{props.children}</MuiButton>
));
<Tooltip title="I can hover" placement="top">
  <Button>Custom Button</Button>
</Tooltip>

According to the Tooltip docs at https://material-ui./api/tooltip/#props, I should make sure that the child can hold a ref, so I have wrapped it in the React.forwardRef function and passed the ref to the Material UI Button ponent that it is returning, but the tooltip still isn't working.

Any ideas what I'm missing?

Share Improve this question asked Apr 9, 2020 at 5:46 zeckdudezeckdude 16.2k44 gold badges148 silver badges194 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

I figured out the solution, so instead of closing the question, I'll leave what I did. So I forgot to spread the props object to the wrapped MUI button and apparently the Tooltip is passing something to the Button that is important to make the hover work. Likely it is the onMouseOver, onMouseLeave, onFocus, onBlur, onTouchStart, and onTouchEnd props that are the issue.

const Button = React.forwardRef((props, ref) => (
  <MuiButton ref={ref} {...props}>
    {props.children}
  </MuiButton>
));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745162664a4614461.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信