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.

Performance drop using useAnimatedScrollHandler

See original GitHub issue

Description

I am noticing quite a considerable amount of lost frames when using the new useAnimatedScrollHandler hook as opposed to the ReanimatedV1 method, it is especially noticeable when pulling down on iOS and letting go to have the bounce back effect, or scrolling quickly.

Steps To Reproduce

reanimatedV2

import React from 'react';
import { Dimensions, View } from 'react-native';
import Animated, {
  useAnimatedScrollHandler,
  useAnimatedStyle,
  useSharedValue,
} from 'react-native-reanimated';

const { width } = Dimensions.get('window');

export default function Facets() {
  const translationY = useSharedValue(0);
  const scrollHandler = useAnimatedScrollHandler((event) => {
    translationY.value = event.contentOffset.y;
  });

  const stylez = useAnimatedStyle(() => {
    return {
      transform: [
        {
          translateY: -translationY.value,
        },
      ],
    };
  });

  return (
    <View style={{ flex: 1 }}>
      <Animated.View
        style={[stylez, { position: 'absolute', zIndex: 10, width, height: 200, backgroundColor: 'red' }]}
      />
      <Animated.ScrollView
        contentContainerStyle={{ paddingBottom: 50, paddingTop: 150 }}
        onScroll={scrollHandler}
        scrollEventThrottle={1}
      >
        <View style={{ height: 1000, width }} />
      </Animated.ScrollView>
    </View>
  );
}

reanimatedV1

import React from 'react';
import { Dimensions, View } from 'react-native';
import Animated from 'react-native-reanimated';

const { width } = Dimensions.get('window');

export default function Facets() {
  const scrollOffset = React.useRef(new Animated.Value(0));
  const stylez = {
    transform: [{ translateY: Animated.multiply(scrollOffset.current, -1) }],
  };

  return (
    <View style={{ flex: 1 }}>
      <Animated.View
        style={[stylez, { position: 'absolute', zIndex: 10, width, height: 200, backgroundColor: 'red' }]}
      />
      <Animated.ScrollView
        contentContainerStyle={{ paddingBottom: 50, paddingTop: 150 }}
        onScroll={Animated.event([{ nativeEvent: { contentOffset: { y: scrollOffset.current } } }])}
        scrollEventThrottle={1}
      >
        <View style={{ height: 1000, width }} />
      </Animated.ScrollView>
    </View>
  );
}

Package versions

    "react": "16.13.1",
    "react-native": "0.63.2",
    "react-native-reanimated": "2.0.0-alpha.6",

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Razorholtcommented, Mar 1, 2021

is there a callback or listener to useAnimatedScrollHandler in ReaminatedV2? Like we have in regular Animated:

Animated.event(
   [{ nativeEvent: { contentOffset: { y: scrollY }}}], 
   {
      useNativeDriver: false,
      listener: event => {
      /* some logic */
      }, 
   }
)
1reaction
jakub-gonetcommented, Sep 17, 2020

Yep, Terry is right. You can try using prebuilt package (under NOTE section) but please, try it tomorrow 😄 We had some problems with creating the package due to case-sensitivity issues and it should be resolved in the next build.

Closing this for now, feel free to reopen if update doesn’t fix it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useAnimatedScrollHandler | React Native Reanimated
This is a convenience hook that returns an event handler reference which can be used with React Native's scrollable components.
Read more >
Animated Collapsible Header Using React Native Reanimated 2
The reason we see frame drops by using react-native's built-in ... useAnimatedScrollHandler This is a convenience hook that returns an event ...
Read more >
React Native Reanimated | FlashList - Shopify
React Native Reanimated is an alternative animation library to the LayoutAnimation API provided by React Native.
Read more >
Getting started with Reanimated 2 for React native Part-1
Reanimated saves us from the frame drop hell in react native ... useSharedValue(0);const scrollHandler = useAnimatedScrollHandler({
Read more >
React Native Animations: a Guide on Creating ... - UpsilonIT
Learn how to make animations in React Native using Reanimated 2 in this guide. ... are easier to use and run on the...
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