首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >资料UI InputLabel使用参考资料

资料UI InputLabel使用参考资料
EN

Stack Overflow用户
提问于 2020-01-18 02:07:00
回答 1查看 1.3K关注 0票数 4

如何访问ref param of InputLabel of @material-ui/core in TypeScript?

下面的代码会导致错误,ref说:

TS2769:没有重载匹配此调用。

代码语言:javascript
复制
export class ComboBox extends React.Component<ComboBoxProps, ComboBoxState> {
    private inputLabelRef = React.createRef();

    public render() {
        return <FormControl fullWidth={this.props.fullWidth} variant='outlined'>
            <InputLabel required={this.props.required}
                        id={this.props.id + '-label'}
                        ref={this.inputLabelRef}>
                {this.props.caption}
            </InputLabel>
            <Select labelId={this.props.id + '-label'}
                    id={this.props.id}
                    value={this.props.value}
                    onChange={(e: any) => this.onChangeSelection(e.target.value)}
                    labelWidth={200}>
                {this.renderItems()}
            </Select>
        </FormControl>;
    }

    ...
}

我已经试过使用泛型,尝试:

代码语言:javascript
复制
private inputLabelRef = React.createRef<InputLabel>();

但是这导致了InputLabel所说的错误:

'InputLabel‘指的是一个值,但被用作here.ts类型(2749)

正在使用折叠式版本:

  • "@material-ui/core":"4.6.1“、
  • "@types/react":"16.9.11”、
  • "@types/react-dom":"16.9.4“、
  • "react":"16.11.0”、
  • "react-dom":"16.11.0“、
  • "typescript":"3.7.4”、
EN

回答 1

Stack Overflow用户

发布于 2020-01-19 08:52:37

InputLabel中的引用被转发到根元素。因此,您需要使用实际的html元素接口键入它。将其改为以下内容应该有效:

代码语言:javascript
复制
private inputLabelRef = React.createRef<HTMLLabelElement>();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59796951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档