How to get onPress event from ScrollView Component in React Native

JackKalish

I am new to React Native. I have an app with a ScrollView component and several custom components inside it. I would like to trigger an onPressOut event from the ScrollView, so that when the user scrolls, and releases, the child components inside the ScrollView update their states. But, the ScrollView documentation does not include any press events: https://facebook.github.io/react-native/docs/scrollview.html

Other things I tried:

  • I tried wrapping the scrollview inside a TouchableWithoutFeedback component, when I do this, I get an error as soon as I try to scroll:

2015-10-26 11:59:13.849 [error][tid:com.facebook.React.ShadowQueue][RCTModuleMethod.m:60] Argument 0 (NSNumber) of RCTUIManager.measure must not be null 2015-10-26 11:59:13.850 [error][tid:com.facebook.React.ShadowQueue][RCTModuleMethod.m:60] Argument 0 () of RCTUIManager.measure could not be processed. Aborting method call.

No idea what that means... Any ideas? Thanks!

Here is my code:

<View ref="theWrapperView" style={styles.theWrapperView}>
            <ScrollView 
                onScroll={this.onScroll.bind(this)}
                onMomentumScrollEnd={this.onScrollAnimationEnd.bind(this)}

                style={styles.scrollView}
                contentContainerStyle={styles.scrollViewContentContainer}

                showsHorizontalScrollIndicator={true}
                scrollEventThrottle={10}
                pagingEnabled={true}
                horizontal={true}
                automaticallyAdjustContentInsets={false}
                snapToAlignment={'center'}
                snapToInterval={20}
                zoomScale={1.5}
                centerContent = {true}

            >
                {cards}
            </ScrollView>
        </View>

When I change the View element to a TouchableWithoutFeedback element is when I get the error.

JackKalish

I have figured this out after investigating the React Native source code: https://github.com/facebook/react-native/blob/master/Libraries/Components/ScrollResponder.js

As it turns out there are actually a number of other events available via the ScrollResponder mixin, that are not mentioned in the online documentation. It is not necessary to include the mixin to access these events, since they are already part of the ScrollView component. The one that was useful for me was onResponderRelease, which fires when you release the ScrollView, like so:

<View ref="theWrapperView" style={styles.theWrapperView}>
            <ScrollView 
                onScroll={this.onScroll.bind(this)}
                onMomentumScrollEnd={this.onScrollAnimationEnd.bind(this)}
                onResponderRelease = {this.onResponderReleaseHandler.bind(this)}
            >
                {cards}
            </ScrollView>
        </View>

and in the class define a handler:

onResponderRelease(){
//do stuff

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

React Native : how to change view on onPress event

From Dev

React Native: TouchableOpacity onPress problems inside a ScrollView

From Dev

How do I bind the storeId to an onPress event in React-Native?

From Dev

Update parent component in react native from component attached to scrollview

From Dev

React Native: onPress on component doesn't function

From Dev

Render a component onPress(TouchableOpacity/Button) in React Native

From Dev

react-native-maps - polygon onPress event

From Dev

React Native, ScrollView Paging event

From Dev

How to get data from the redux store and use it in component in React Native

From Dev

How to get size of a component in react-native?

From Dev

react-native onPress() toggles automatically on a loop component rendering

From Dev

this.state is undefined during onPress event in react native

From Dev

How can I set onPress event in a child component?

From Dev

React native: How to individually control each onpress() from map() generated components

From Dev

How to get reference to the dispatch method from a component method in my react-native redux app

From Java

Get current scroll position of ScrollView in React Native

From Dev

How to get react native version from JavaScript?

From Java

react native how to call multiple functions when onPress is clicked

From Dev

React Native - how to change style and image of a view onPress

From Dev

React Native - How to measure size of content in ScrollView?

From Dev

React: How to get results from one component to another component

From Dev

How to open links in Safari from React Native's WebView component

From Dev

How to open links in Safari from React Native's WebView component

From Dev

React Native- How to pass state from a component to it's parent

From Dev

How to pass back argument from child component on React Native

From Dev

How to include view to component from another file in react native?

From Dev

React Native Hamburger onPress Issue

From Dev

react native passing onPress with value

From Dev

React Native,not correct working onPress

Related Related

  1. 1

    React Native : how to change view on onPress event

  2. 2

    React Native: TouchableOpacity onPress problems inside a ScrollView

  3. 3

    How do I bind the storeId to an onPress event in React-Native?

  4. 4

    Update parent component in react native from component attached to scrollview

  5. 5

    React Native: onPress on component doesn't function

  6. 6

    Render a component onPress(TouchableOpacity/Button) in React Native

  7. 7

    react-native-maps - polygon onPress event

  8. 8

    React Native, ScrollView Paging event

  9. 9

    How to get data from the redux store and use it in component in React Native

  10. 10

    How to get size of a component in react-native?

  11. 11

    react-native onPress() toggles automatically on a loop component rendering

  12. 12

    this.state is undefined during onPress event in react native

  13. 13

    How can I set onPress event in a child component?

  14. 14

    React native: How to individually control each onpress() from map() generated components

  15. 15

    How to get reference to the dispatch method from a component method in my react-native redux app

  16. 16

    Get current scroll position of ScrollView in React Native

  17. 17

    How to get react native version from JavaScript?

  18. 18

    react native how to call multiple functions when onPress is clicked

  19. 19

    React Native - how to change style and image of a view onPress

  20. 20

    React Native - How to measure size of content in ScrollView?

  21. 21

    React: How to get results from one component to another component

  22. 22

    How to open links in Safari from React Native's WebView component

  23. 23

    How to open links in Safari from React Native's WebView component

  24. 24

    React Native- How to pass state from a component to it's parent

  25. 25

    How to pass back argument from child component on React Native

  26. 26

    How to include view to component from another file in react native?

  27. 27

    React Native Hamburger onPress Issue

  28. 28

    react native passing onPress with value

  29. 29

    React Native,not correct working onPress

HotTag

Archive