Scroll to a Specific Item in ScrollView List View

Scroll to a Specific Item in ScrollView List View

In this Example, we will see how to Scroll to a Specific Item in ScrollView ListView. If you are not clear with the topic then you can imagine you have made a list using scroll view something like the example of Making a List using ScrollView and now you want to scroll to a specific item in the ScrollView list. For example, you are searching any data from the ScrollView List array and found the matching data on index 7 in the array now you want to scroll the list to item 7, In this situation, you can take the help from this example.

This post is a solution for the most recent query asked by one of our AboutReact family member Pranjul Mishra. While solving the query I found something interesting so I decided to make a post on it.

Note: We are not going to use any external library to do this. We are going to use onLayout prop of the View component provided by React Native.

In this example, we will create a List using Scroll View to hold the data, a TextInput and a button to take the index as input and scroll to the item.

Scroll to the Specific item

1. To scroll to the specific item first we will make a blank array in the constructor to store the X and Y coordinates of the item.

2. While rendering the item we will store the X and Y location of the item in the array. These locations can be found using the onLayout prop of the view Component. We have also added a reference to the ScrollView with name scrollview_ref.

3. After the 2nd step, you have a ScrollView list with the data listed from the array and an array with the name arr which holds the X and Y location of the item on the same index as the data array has. Now whenever we want to scroll to a specific location we can use scrollTo which is a property of ScrollView. In this we have to pass the X and Y location to scroll and animated (True/False).

That is it. 🙂

Now you can see the full example code below.

To Make a React Native App

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. Open the terminal and go to the workspace and run
npm install -g react-native-cli
Run the following commands to create a new React Native project
react-native init ProjectName
This will make a project structure with an index file named App.js in your project directory.

Now Open App.js in any code editor and replace the code with the following code.

App.js

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

Android

      
   

IOS

         

      

This is how you can Scroll to a Specific Item in ScrollView ListView. If you have any doubt or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

How useful was this post?

Click on a star to rate us!

Average rating / 5. Vote count:

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

About React will use the information you provide on this form to be in touch with you and to provide updates and marketing.