question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[ScrollView] Use `onScroll` without event throttling

See original GitHub issue

I want to use the onScroll handler to setState another component in relation to the ScrollView’s contentOffset changing its value.

But the throttling of onScroll causes noticeable jumps. Sometimes the component that has setState called on it will never even update until much later on.

Is there a better way to achieve what I want?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:23 (10 by maintainers)

github_iconTop GitHub Comments

5reactions
marcshillingcommented, Feb 15, 2016

@zoharlevin nope, ended up changing our design requirements because we couldn’t get the performance 😦

@aleclarson yes, the problem is still apparent in a bundled release build.

Unless I’m missing something, I think this example is an example where React Native is just not gonna cut it performance-wise. Will have to drop down to a native scroll view.

5reactions
yamillcommented, Aug 5, 2015

I think what you’re looking for is something like this:

onScroll={Animated.event(
[{nativeEvent: {contentOffset: {y: this.state.yValue}}}]
)}

You can then animate your view using the state values & interpolate. I do this for a pull to refresh animation inside my renderHeader function:


<View style={{height: 100, justifyContent: 'center', alignItems: 'center', marginTop: -100}}>
              <Animated.View style={{
                backgroundColor: colors.white,
                width: 40,
                height: 40,
                borderRadius: 20,
                alignItems: 'center',
                justifyContent: 'center',
                opacity: this.state.yValue.interpolate({ inputRange: [-80,0], outputRange: [1, 0]}),
                transform: [
                  {scale: this.state.yValue.interpolate({ inputRange: [-80,0], outputRange: [1, .5]}),
                extrapolate: 'extend'}
                ]}}>
                <Text style={{color: colors.black, fontSize: 16}}>RCT</Text>
              </Animated.View>
 </View>

Read more comments on GitHub >

github_iconTop Results From Across the Web

Quick Tip: How to Throttle Scroll Events - SitePoint
This technique helps with checking if we are at a specific part of the page. Given the size of the page, it takes...
Read more >
Scroll event firing too many times. I only want it to fire a ...
This will fire the first scroll event immediately and then get you a scroll event ... You can use Throttling fonction calls: throttling-function-calls...
Read more >
ScrollView - React Native
scrollEventThrottle. iOS. ​ ... This controls how often the scroll event will be fired while scrolling (as a time interval in ms). A...
Read more >
Element: scroll event - Web APIs | MDN
The setTimeout() method is used to throttle the event handler because scroll events can fire at a high rate. For additional examples that ......
Read more >
Throttling Scroll Events in JavaScript - John Kavanagh
Care needs to be taken when using the onScroll event; it can trigger frequently, leading to greater client-side resource (CPU) usage, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found