Bug report.
Yes
TypeScript related.
Yes
info Fetching system and libraries information...
System:
OS: macOS 10.15.5
CPU: (16) x64 AMD Ryzen 7 1700X Eight-Core Processor
Memory: 166.43 MB / 16.00 GB
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 12.18.0 - ~/.nvm/versions/node/v12.18.0/bin/node
Yarn: 1.22.4 - ~/.nvm/versions/node/v12.18.0/bin/yarn
npm: 6.14.4 - ~/.nvm/versions/node/v12.18.0/bin/npm
SDKs:
iOS SDK:
Platforms: iOS 13.5, DriverKit 19.0, macOS 10.15, tvOS 13.4, watchOS 6.2
IDEs:
Xcode: 11.5/11E608c - /usr/bin/xcodebuild
npmPackages:
react: 16.13.1 => 16.13.1
react-native: https://github.com/expo/react-native/archive/sdk-37.0.0.tar.gz => 0.61.4
That TypeScript does not error when using useRef.
TypeScript warns that refs are not compatible.
const carouselRef = useRef<Carousel<Item>>(null)
<Carousel ref={carouselRef} />
Will error:
No overload matches this call.
Overload 1 of 2, '(props: Readonly<CarouselProperties<Item>>): Carousel<Item>', gave the following error.
Type 'MutableRefObject<Carousel<Item>>' is not assignable to type 'string | (string & ((instance: ScrollViewProps) => void)) | (string & RefObject<ScrollViewProps>) | (string & ((instance: CarouselStatic<Item>) => void)) | ... 22 more ... | (RefObject<...> & ... 1 more ... & RefObject<...>)'.
Type 'MutableRefObject<Carousel<Item>>' is not assignable to type 'React.RefObject<...> & React.RefObject<...> & React.RefObject<...>'.
Type 'MutableRefObject<Carousel<Item>>' is not assignable to type 'RefObject<CarouselStatic<Item>>'.
Types of property 'current' are incompatible.
Type 'Carousel<Item>' is missing the following properties from type 'CarouselStatic<Item>': currentIndex, currentScrollPosition, startAutoplay, stopAutoplay, and 4 more.
Overload 2 of 2, '(props: CarouselProperties<Item>, context?: any): Carousel<Item>', gave the following error.
Type 'MutableRefObject<Carousel<Item>>' is not assignable to type 'string | (string & ((instance: ScrollViewProps) => void)) | (string & RefObject<ScrollViewProps>) | (string & ((instance: CarouselStatic<Item>) => void)) | ... 22 more ... | (RefObject<...> & ... 1 more ... & RefObject<...>)'.
Type 'MutableRefObject<Carousel<Item>>' is not assignable to type 'React.RefObject<...> & React.RefObject<...> & React.RefObject<...>'.
Type 'MutableRefObject<Carousel<Item>>' is not assignable to type 'RefObject<CarouselStatic<Item>>'.
I also tried typing the useRef with CarouselStatic but this didn't help.
I see that I can also do:
<Carousel ref={(ref) => (carouselRef.current = ref)} />
Although I am not sure the typings are correct?
I also have such problem in my code
I was trying to use it like
const carouselRef = useRef<CarouselStatic<CarouselPage[]>>();
<Carousel ref={carouselRef} />
^^^^^^^^^^^ - shows warning
I also have problem too.
I trying with pagination
const ref= useRef<any>();
//Here works
<Carousel ref={ref}/>
// Here not works
<Pagination carouselRef={ref} />
I try other ways like:
const ref= useRef<CarouselStatic<any>>();
const ref= useRef<Flatlist<any>>();
But unsuccefully
I'm getting the same issue, I needed a workout for this at the moment which was to just declare any on the ref. Awful but I needed the build to pass.
const carouselRef: any = useRef(null)
//
carouselRef.current.snapToItem(index);
const ref = useRef()
ref.current.anything is undefined
what to do 馃槶
Hey folks.
const carouselRef = useRef<Carousel<Item>>(null);
This way worked for me. I had to initialize ref with null value, and the warning is gone.
I hope this can be usefull for you.
Hey folks.
const carouselRef = useRef<Carousel<Item>>(null);This way worked for me. I had to initialize
refwithnullvalue, and the warning is gone.I hope this can be usefull for you.
Sorry but what is Item meaning here? Can you let me know please 馃檹
@cuongn265 Item is the type that you want to use in carousel list, can be a TypeScript class, interface or type.
Most helpful comment
Hey folks.
const carouselRef = useRef<Carousel<Item>>(null);This way worked for me. I had to initialize
refwithnullvalue, and the warning is gone.I hope this can be usefull for you.