我是靠谱客的博主 冷艳音响,这篇文章主要介绍React.ClassAttributes<HTMLDivElement>.ref?: React.LegacyRef<HTMLDivElement>解决方案,现在分享给大家,希望可以做个参考。

(JSX attribute) React.ClassAttributes.ref?: React.LegacyRef
Type ‘MutableRefObject’ is not assignable to type ‘LegacyRef’.
Type ‘MutableRefObject’ is not assignable to type ‘RefObject’.
Types of property ‘current’ are incompatible.
Property ‘align’ is missing in type ‘HTMLElement’ but required in type ‘HTMLDivElement’.ts(2322)
lib.dom.d.ts(6584, 5): ‘align’ is declared here.
index.d.ts(143, 9): The expected type comes from property ‘ref’ which is declared here on type ‘DetailedHTMLProps<HTMLAttributes, HTMLDivElement>’

解决方案

可以创建的是时候指定泛型类型:

React.createRef();

不要使用HTMLElement

ref如果用的是这种回调函数

ref={elem => this.wrapper = elem}
this.wrapper直接代表dom节点
ref如果用下面这种方式
ref={this.wrapper}
this.wrapper = React.createRef();
this.wrapper.current代表dom元素…
说的可能不太专业, 反正上面的代码this.wrapper替换成this.wrapper.current就可以了

最后

以上就是冷艳音响最近收集整理的关于React.ClassAttributes<HTMLDivElement>.ref?: React.LegacyRef<HTMLDivElement>解决方案的全部内容,更多相关React.ClassAttributes<HTMLDivElement>.ref?:内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部