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
};
});
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>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With