(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?:内容请搜索靠谱客的其他文章。
发表评论 取消回复