Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()).

Property 'setTabChange' does not exist on type 'never'.

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
  };
});
like image 264
Chinnu Avatar asked Jul 30 '26 05:07

Chinnu


1 Answers

As useRef is generic when it is used with TypeScript, you have to define the referenced element type like const ref = useRef<Type>();

So to fix this error you can define the ref like

const ref = useRef<any>();
like image 85
theWellHopeErr Avatar answered Aug 01 '26 19:08

theWellHopeErr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!