如何解决如何在不使用forwardedRef的情况下使用打字稿在react hoc中创建ref
这是我的代码:
import React,{
InputHTMLAttributes,Component,ComponentClass,TextareaHTMLAttributes
} from 'react';
// type TElementType = HTMLInputElement | HTMLTextAreaElement;
// type TAttributesType =
// | InputHTMLAttributes<HTMLInputElement>
// | TextareaHTMLAttributes<HTMLTextAreaElement>;
interface IClassType {
somevalue: string;
}
type TElementType = HTMLInputElement;
type TAttributesType = InputHTMLAttributes<HTMLInputElement>;
const withInput = (Comp: ComponentClass<TAttributesType>): ComponentClass<IClassType> => {
class MyInput extends Component<IClassType> {
private refValue = React.createRef<TElementType>();
componentDidMount() {
console.log('do something');
}
render() {
return (
<>
<Comp
name="someinput"
value="somevalue"
type="text"
ref={ref => {
this.refValue = ref;
}}
/>
</>
);
}
}
return MyInput;
};
export default withInput;
这是此错误。refValue = ref:
Type'Component
| “ null”不可分配给“ RefObject”类型。 不能将类型“ null”分配给类型“ RefObject”。
我在网上看过,只能找到forwardedRefs的示例。我不需要从传递的组件中转发引用,因为该引用仅在我的临时容器中是必需的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。