简体   繁体   English

“从不”类型上不存在属性“setTabChange”。 (React hooks Typescript) 使用 ref

[英]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.

相关问题 反应钩子和打字稿-类型&#39;从不&#39;属性&#39;***&#39;不存在 - react hooks and typescript - Property '***' does not exist on type 'never' 带有 Typescript 的 React Hooks - 类型上不存在属性 - React Hooks with Typescript - Property does not exist on type React、Typescript、Hooks - 类型上不存在属性“状态” - React, Typescript, Hooks - Property 'state' does not exist on type 使用 typescript 反应钩子:类型上不存在属性“数据” - React Hooks with typescript: Property 'data' does not exist on type 类型'A'.ts(2339)上不存在属性'ref' - React,TypeScript - Property 'ref' does not exist on type 'A'.ts(2339) - React, TypeScript React TypeScript 和 ForwardRef - 类型“IntrinsicAttributes”上不存在属性“ref” - React TypeScript & ForwardRef - Property 'ref' does not exist on type 'IntrinsicAttributes Typecript属性''在类型'never'上不存在。在React NodeJs堆栈中 - Typescript Property '' does not exist on type 'never'. in React NodeJs stack 在 React 中上传文件 / Typescript -> 属性“click”在类型“never”上不存在 - upload File in React / Typescript -> Property 'click' does not exist on type 'never' state 上不存在属性 - 将 React Router Hooks 与 TypeScript 结合使用 - Property does not exist on state - Using React Router Hooks with TypeScript Typescript 另一个 Ref 的 Ref - 类型上不存在属性 - Typescript Ref of another Ref - Property does not on exist on type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM