React.ClassAttributes<HTMLDivElement>.ref?: React.LegacyRef<HTMLDivElement> àì夳堔傛蜴生んèń 2022-11-16 05:43 156阅读 0赞 (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就可以了
还没有评论,来说两句吧...