Material-ui: [IconButton] [Typescript] ref property not available

Created on 22 Nov 2017  路  3Comments  路  Source: mui-org/material-ui

Property 'ref' does not exist on type 'IntrinsicAttributes & IconButtonProps & { children?: ReactNode; }'

This is the case for IconButton, but might be the case for others as well (didn't test any others).

  • [x] I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

The ref property should be available for any react component.

Current Behavior

It is not, making TS complain with a 'Property 'ref' does not exist on type 'IntrinsicAttributes & IconButtonProps & { children?: ReactNode; }' when trying to add it.

Steps to Reproduce (for bugs)

Create a tsx file that uses the following:

      <IconButton
        ref={node => { console.log(node); }}
      >
      </IconButton>

Context

Your Environment

| Tech | Version |
|--------------|---------|
| Material-UI | latest beta |
| React | 16 |
| browser | Chrome 62 |
| ts| 2.6.1 |

IconButton typescript

Most helpful comment

This is because IconButton is declared as a React.ComponentType instead of a React.ComponentClass; we're purposely abstracting away whether IconButton even has an instance associated with it. It happens that it does, but it's probably not what you want; its instance is a result of the withStyles HOC. Are you sure you don't want either the rootRef or buttonRef props?

All 3 comments

This is because IconButton is declared as a React.ComponentType instead of a React.ComponentClass; we're purposely abstracting away whether IconButton even has an instance associated with it. It happens that it does, but it's probably not what you want; its instance is a result of the withStyles HOC. Are you sure you don't want either the rootRef or buttonRef props?

Actually I wanted buttonRef as target anchor element for a popover :) Thanks

is it fixed? why close it? because i still get the warning.
constructor(props) { super(props) this.myRef = React.createRef() }
i get the warning: [at-loader] property "myRef" does not exist on type ...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

amcasey picture amcasey  路  70Comments

damianobarbati picture damianobarbati  路  55Comments

nathanmarks picture nathanmarks  路  100Comments

NonameSLdev picture NonameSLdev  路  56Comments

iceafish picture iceafish  路  62Comments