Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding type for useRef on ScrollView react-native

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.

like image 407
Kieran Osgood Avatar asked Oct 26 '20 07:10

Kieran Osgood


People also ask

What is ScrollView in React Native?

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

What is useref in react hook?

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.

How do I get Started with React Native?

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.

How do I Create A ref to a ScrollView component?

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?


Video Answer


2 Answers

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!

like image 146
Luiz Baldi Avatar answered Oct 17 '22 16:10

Luiz Baldi


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);
like image 38
Mr.Jerry. Avatar answered Oct 17 '22 16:10

Mr.Jerry.