Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warning when using a custom component in Button #15171

Closed
eps1lon opened this issue Apr 3, 2019 · 5 comments
Closed

Warning when using a custom component in Button #15171

eps1lon opened this issue Apr 3, 2019 · 5 comments
Labels
bug 🐛 Something doesn't work component: button This is the name of the generic UI component, not the React module! typescript
Milestone

Comments

@eps1lon
Copy link
Member

eps1lon commented Apr 3, 2019

Hi, I am having trouble to type Button with Link from react-router v5, as seen in https://next.material-ui.com/demos/buttons/#third-party-routing-library, maybe some one could add the ts version of this example.

tried:

<Button component={({ ref, ...props }) => <Link to="/about" {...props} ref={ref} />}>
   about
</Button>

No issue in the editor.
but warning from material-ui

 Warning: Failed prop type: Invalid prop `component` supplied to `ButtonBase`. Expected an element type that can hold a ref.

followed by warning from react

Warning: component: `ref` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop.

using typescrupt:3.3.4000, react:latest, material-ui: next(latest)

Originally posted by @stunaz in #14897 (comment)

@eps1lon eps1lon added component: button This is the name of the generic UI component, not the React module! docs Improvements or additions to the documentation labels Apr 3, 2019
@eps1lon
Copy link
Member Author

eps1lon commented Apr 3, 2019

Warning: Failed prop type: Invalid prop component supplied to ButtonBase. Expected an element type that can hold a ref.

Is this the full error message? There should be link to https://next.material-ui.com/guides/composition/#caveat-with-refs which explains the issue.

To be specific you forgot to include the part from https://next.material-ui.com/demos/buttons/#third-party-routing-library about React.forwardRef

@stunaz
Copy link
Contributor

stunaz commented Apr 3, 2019

Thanks for paying attention to this issue. I will try and provide a sandbox. I was getting more error trying using React.forwardRef

Edit: sandbox ==> open the console to see the warning

@eps1lon
Copy link
Member Author

eps1lon commented Apr 3, 2019

The first usage is wrong in Material-UI as well as react (without forwardRef).

The second one is a typescript issue only with how the props in forwardRef are typed. Using props: any solves this but isn't desirable.

We need a test case for forwardRef components in component and a fix.

@eps1lon eps1lon added bug 🐛 Something doesn't work typescript and removed docs Improvements or additions to the documentation labels Apr 3, 2019
@eps1lon eps1lon added this to the v4 milestone Apr 3, 2019
@eps1lon
Copy link
Member Author

eps1lon commented Apr 4, 2019

@stunaz
This works already today in your codesandbox:

<Button
  component={React.forwardRef<HTMLAnchorElement, Partial<LinkProps>>(
    (props, ref) => (
      <Link to="/about" {...props} ref={ref as any} />
    )
  )}
>
  with React.ForwardRef (followed in mui docs)
</Button>

ref as any is an issue with @types/react-router-dom and should be fixed with DefinitelyTyped/DefinitelyTyped#34464.

@stunaz
Copy link
Contributor

stunaz commented Apr 4, 2019

Nice, Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: button This is the name of the generic UI component, not the React module! typescript
Projects
None yet
Development

No branches or pull requests

2 participants