react-native-reanimated: Animated ScrollView _this.props.OnScroll is not a function

Description

I would like to animate a React Navigation header based on scrolling an Animated.ScrollView. Initially this works perfect. Although, when developing and the application is hot reloading, the scrolling seems to breaks. It looks like the props.onScroll method all of a sudden removed from the ScrollView (see screenshot).

It’s hard to debug what to do about this, so I was hoping anyone here has any clue?

In the repository these are the relevant files:

Screenshot 2021-10-12 at 15 08 05

Expected behavior

On refresh (Hot Reload) the scroll behaviour should be exactly the same as before hot reloading.

Actual behavior & steps to reproduce

  1. Download the repo
  2. npm install && cd ios && pod install
  3. Open xcode
  4. Run project in simulator with scheme “Test - Debug”
  5. On the Onboarding screen press “Go”
  6. Next page “Dashboard” is as explained above

Snack or minimal code example

See the repository, branch moti-tryout

Package versions

  • React Native: “react-native”: “0.65.1”,
  • React Native Reanimated: “react-native-reanimated”: “2.2.2”,
  • NodeJS: v14.16.0
  • Xcode: Version 13.0 (13A233)
  • Java & Gradle: - (only iOS for now)

React Navigation:

Affected platforms

  • Android
  • iOS
  • Web

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 15

Most upvoted comments

I just had this error with Animated.FlatList. I updated the package to 2.4.1, tried creating animated component with Animated.createAnimatedComponent but it still didn’t work. I tried using multiple events like

const scrollHandler = useAnimatedScrollHandler({ onScroll: event => { scrollY.value = event.contentOffset.y; }, onBeginDrag: e => { scrollY.value = true; }, onEndDrag: e => { scrollY.value = false; } });

and tried using single onScroll event but still, I got this error. I didn’t try it on ScrollView since I really depend on pullToRefresh and onEndReached which ScrollView doesn’t have. The only thing I was able to find is that useAnimatedScrollHandler returns an object instead of a function which creates this error. When I tried logging the object I got this:

{"current": {"eventNames": ["onScroll"], "listeners": {}, "reattachNeeded": false, "registrations": [], "viewTag": undefined, "worklet": [Function _f]}}

Maybe someone can elaborate what is going on and help with the issue. Thanks in advance.

Check if you’re importing Animated from react-native-reanimated and not react-native. After changing that I stopped getting this error.

For those facing the same issue recently, try to set useNativeDriver to false in Animated.event’s config like this:

Animated.event(
            [{
                nativeEvent: {
                ...
             },],
            // if use native driver, then cause an error
            { useNativeDriver: false },)

This solved the issue on my side, but I don’t know what triggered this exactly.

Animated is not present in react-native-reanimated not getting any suggestions in vscode

it’s a default export so you have to do something like this :

import Animated, { useAnimatedScrollHandler, useAnimatedStyle, useSharedValue, withSpring, withTiming } from 'react-native-reanimated'

or just

import Animated from 'react-native-reanimated'


Here’s one case that can lead into this error :

import { SectionList } from 'react-native'
import Animated, { useAnimatedScrollHandler, withSpring  } from 'react-native-reanimated'


const scrollHandler = useAnimatedScrollHandler({
    onScroll: event => {
      scrollY.value = withSpring(event.contentOffset.y)
    }
})

return (
<Animated.View style={[styles.contentHeaderWrapper, opacityStyle]}>
<SectionList
            contentContainerStyle={[styles.content, { paddingBottom: bottomInset }]}
            keyExtractor={(item, index) => `${index}`}
            onScroll={scrollHandler} // <-- check this 
            renderItem={renderRecipe}
            renderSectionHeader={renderSectionTitle}
            scrollEventThrottle={16}
            sections={recipes}
            showsVerticalScrollIndicator={false}
            stickySectionHeadersEnabled={false}
/>
</Animated.View>

Here SectionList is from react-native but the scroll handler is from react-native-reanimated so it will throw an error

I just had this error with Animated.FlatList. I updated the package to 2.4.1, tried creating animated component with Animated.createAnimatedComponent but it still didn’t work. I tried using multiple events like

const scrollHandler = useAnimatedScrollHandler({ onScroll: event => { scrollY.value = event.contentOffset.y; }, onBeginDrag: e => { scrollY.value = true; }, onEndDrag: e => { scrollY.value = false; } });

and tried using single onScroll event but still, I got this error. I didn’t try it on ScrollView since I really depend on pullToRefresh and onEndReached which ScrollView doesn’t have. The only thing I was able to find is that useAnimatedScrollHandler returns an object instead of a function which creates this error. When I tried logging the object I got this:

{"current": {"eventNames": ["onScroll"], "listeners": {}, "reattachNeeded": false, "registrations": [], "viewTag": undefined, "worklet": [Function _f]}}

Maybe someone can elaborate what is going on and help with the issue. Thanks in advance.

Make sure to import Animated from react-native-reanimated instead of react-native!

- import { Animated } from "react-native"
+ import Animated from "react-native-reanimated"

Use <Animated.ScrollView> instead of just regular ScrollView

Tip: Make sure the component you’re using onScroll comes from Animated (ref)