Get current scroll position of ScrollView in React Native

后端 未结 11 1520
遇见更好的自我
遇见更好的自我 2020-11-27 11:10

Is it possible to get the current scroll position, or the current page of a component in React Native?

So something like:

<         


        
11条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 11:34

    Brad Oyler's answer is correct. But you will only receive one event. If you need to get constant updates of the scroll position, you should set the scrollEventThrottle prop, like so:

    
      
        Be like water my friend …
      
    
    

    And the event handler:

    handleScroll: function(event: Object) {
      console.log(event.nativeEvent.contentOffset.y);
    },
    

    Be aware that you might run into performance issues. Adjust the throttle accordingly. 16 gives you the most updates. 0 only one.

提交回复
热议问题