Looking to create a ref to a ScrollView component like so: const ref = useRef<ScrollView>(null);
(or something to this effect) essentially I need access to the flashScrollIndicators
method like this:
<ScrollView
ref={ref}
onContentSizeChange={() => ref?.current?.flashScrollIndicators()}>
{children}
</ScrollView>
The code does work in my app however typescripts definitions for ScrollView doesn't seem to include flashScrollIndicators?
I'm getting Property 'flashScrollIndicators' does not exist on type 'ScrollView'.ts(2339)
looking through the types I can see SectionList and Flatlist so unsure if this is an bug with the definitions or just me not using it properly.
The ScrollView is a built-in React Native generic scrolling container that can host multiple components and views. The scrollable items need not be homogeneous, and you can scroll both vertically and horizontally (by setting the horizontal property).
Mutable values useRef (initialValue) is a built-in React hook that accepts one argument as the initial value and returns a reference (aka ref ). A reference is an object having a special property current.
Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility.
Looking to create a ref to a ScrollView component like so: const ref = useRef<ScrollView> (null); (or something to this effect) essentially I need access to the flashScrollIndicators method like this: The code does work in my app however typescripts definitions for ScrollView doesn't seem to include flashScrollIndicators?
After some research, I couldn't find any official documentation or blog post with the correct solution but declaring the ref using ScrollView itself worked for me. So you can do something like:
const scrollViewRef = React.useRef<ScrollView>(null);
And then autocomplete will take care of the rest, hope it helps!
Since the flashscrollindicators
does exist on ScrollView in documentation (and works in app) https://reactnative.dev/docs/scrollview#flashscrollindicators I assume this is bug in type definition.
I think the cleanest solution is to define your own ScrollViewRef using intersection.
import { ScrollView } from 'react-native';
export type ScrollViewRef = ScrollView & {
flashScrollIndicators: () => void;
};
then use this type to define the ref
const scrollViewRef = React.useRef<ScrollViewRef | null>(null);
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