Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple onScroll Events on ScrollView React Native?

Is it possible to have multiple onScroll events in React Native's ScrollView?

The problem i'm having is that I have a component that renders a ListView like so:

<ListView
      onScroll={Animated.event(
        [{nativeEvent: {contentOffset: {y: this.state.scrollY}}}],
        {onScroll: this.props.onScroll}
      )}
      scrollEventThrottle={10}
      dataSource={stuff}
      removeClippedSubviews={false}
      renderSeparator={this.makeSeparator}
      renderRow={this.makeStuff}
      enableEmptySections={true}
    />

As you can see there's already an Animaion event subscribed to it, but I also want to be able to pass this.props.onScroll in case I want different scroll functionality for higher components that render this one.

Is this possible? Any ideas?

like image 974
Elliott McNary Avatar asked Jun 02 '17 19:06

Elliott McNary


2 Answers

if you are using "useNativeDriver":

onScroll = Animated.event(
        [{nativeEvent: {contentOffset: {x: this.xOffset}}}],
        {
            listener: (event)=>{
                //do something here like Keyboard.dismiss() or this.props.onScroll(event)
            },
            useNativeDriver: true
        }
    );
like image 181
Amir Khorsandi Avatar answered Nov 12 '22 04:11

Amir Khorsandi


If you're looking to combine events, I'd just take the Animated.event out into its own method. So

<ListView
      onScroll={this.onScroll.bind(this)}
      ...
      />

onScroll(event) {
    if(this.props.onScroll) this.props.onScroll(event)
    Animated.event(
        [{nativeEvent: {contentOffset: {y: this.state.scrollY}}}],
        {onScroll: this.props.onScroll}
    )(event)
}
like image 4
Greg Billetdeaux Avatar answered Nov 12 '22 05:11

Greg Billetdeaux