[英]Property 'setTabChange' does not exist on type 'never'. (React hooks Typescript) using ref
While calling child component function from parent component usingref(ref.current?.setTabChange())
.在使用
usingref(ref.current?.setTabChange())
从父组件调用子组件函数时。
Property 'setTabChange' does not exist on type 'never'.
“从不”类型上不存在属性“setTabChange”。
Using react typescript.使用反应打字稿。
Parent Component :父组件:
const ref = useRef();
<Tabs ref = {ref} selectedTabIndex={ data.isevaluation === "true" ? 2 : tabIndex !== -1 ? tabIndex : 0} onTabChange={() => {ref.current?.setTabChange()}} >
Child Component :子组件:
const setTabChange = () => {
setSelectedIndex(selectedTabIndex);
};
useImperativeHandle(ref, () => {
return {
setTabChange: setTabChange
};
});
As useRef
is generic when it is used with TypeScript, you have to define the referenced element type like const ref = useRef<Type>();
由于
useRef
在与 TypeScript 一起使用时是通用的,因此您必须定义引用的元素类型,如const ref = useRef<Type>();
So to fix this error you can define the ref
like因此,要修复此错误,您可以像这样定义
ref
const ref = useRef<any>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.