React Native Ref Property 'ref' Does Not Exist On Type 'intrinsicattributes &
Solution 1:
When you log slider.current ref it displayed null because your AppIntroSlider component is a functional component and doesn't support this way. you have two solutions, change AppIntroSlide to a class component and it will work fine, or use forwardRef.
Solution 2:
I took a look at the example that you posted with useImperativeHandle
and you've got it mostly right. Your usage is a little different than the one in my other answer because your function goToSlide
takes arguments.
When you define the interface
for the referenced component, you need to define the goToSlide
function with the appropriate argument types. You currently defined it as a function that takes no arguments (goToSlide(): void
) and that's why you are getting the error "Type '(pageNum: number, triggerOnSlideChange?: boolean | undefined) => void' is not assignable to type '() => void'." on the line with useImperativeHandle
.
exportinterfaceMyRef {
goToSlide(pageNum: number, triggerOnSlideChange?: boolean): void;
}
A bunch of the props on MyCustomComponentProps
should be defined as optional. You are already setting default value for them.
After fixing those two things, all of your errors go away except for those caused by the optional chaining ?.
. This is a new-ish feature and I'm not sure how to get Expo to understand it.
You don't technically need the ?.
after sliderRef
because the ref object will always be defined. It's the current
property that might be null
, so you do need the ?.
after current
. But you could also check it the old-fashioned way if the red underlines bother you:
const prev = (activeIndex: number): void => {
if ( sliderRef.current ) {
sliderRef.current.goToSlide(activeIndex - 1, true);
}
};
Post a Comment for "React Native Ref Property 'ref' Does Not Exist On Type 'intrinsicattributes &"