Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect ScrollView has reached the end

I have a Text with long text inside a ScrollView and I want to detect when the user has scrolled to the end of the text so I can enable a button.

I've been debugging the event object from the onScroll event but there doesn't seem any value I can use.

like image 290
Eldelshell Avatar asked Dec 09 '16 09:12

Eldelshell


People also ask

How does React Native detect scroll?

To get the current scroll position of ScrollView in React Native, we can set the onScroll prop of the ScrollView to a function that takes the scroll event object as an argument.

How can I tell if ScrollView is scrolling Android?

Do you know if it is possible to know if an Android Widget ScrollView can scroll? If it has enough space it doesn't need to scroll, but as soon as a dimension exceeds a maximum value the widget can scroll.

How do I find scroll react?

To detect when a user scrolls to bottom of div with React, we can check if the sum of the scrollTop and clientHeight properties of a scrollable element is equal to the scrollHeight property of the same element. We call the useRef hook to create a ref and we assign the returned ref to the inner div, which is scrollable.


1 Answers

I did it like this:

import React from 'react'; import {ScrollView, Text} from 'react-native';      const isCloseToBottom = ({layoutMeasurement, contentOffset, contentSize}) => {   const paddingToBottom = 20;   return layoutMeasurement.height + contentOffset.y >=     contentSize.height - paddingToBottom; };      const MyCoolScrollViewComponent = ({enableSomeButton}) => (   <ScrollView     onScroll={({nativeEvent}) => {       if (isCloseToBottom(nativeEvent)) {         enableSomeButton();       }     }}     scrollEventThrottle={400}   >     <Text>Here is very long lorem ipsum or something...</Text>   </ScrollView> );      export default MyCoolScrollViewComponent; 

I wanted to add paddingToBottom because usually it is not needed that ScrollView is scrolled to the bottom till last pixel. But if you want that set paddingToBottom to zero.

like image 161
Henrik R Avatar answered Oct 05 '22 23:10

Henrik R