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.

onGestureEvent not working on FlatList

See original GitHub issue

I’m trying to get the gesture from a FlatList because I need the translation.y and not the contentOffset.y given by onScroll event. This is because I’m translating the FlatList and hiding the header as the user scrolls and using the offset provokes glitches (video and source code)

Current FlatList types says props can include NativeViewGestureHandlerProperties, this means onGestureEvent and onHandlerStateChange are available. So this is posible:

import { FlatList } from "react-native-gesture-handler";

<FlatList
      onGestureEvent={() => console.log("onGestureEvent")}
      onHandlerStateChange={() => console.log("onHandlerStateChange")}
      keyExtractor={keyExtractor}
      data={data}
      renderItem={renderItem}
    />

The truth is neither onGestureEvent or onHandlerStateChange are called as you scroll.

I’m using latest version of react-native-gesture-handler (v1.6.1) and this is my stack:

    System:
      OS: macOS 10.15.5
      Shell: 5.7.1 - /bin/zsh
    Binaries:
      Node: 12.14.1 - ~/.nvm/versions/node/v12.14.1/bin/node
      Yarn: 1.22.4 - ~/.nvm/versions/node/v12.14.1/bin/yarn
      npm: 6.13.4 - ~/.nvm/versions/node/v12.14.1/bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    IDEs:
      Android Studio: 3.6 AI-192.7142.36.36.6392135
      Xcode: 11.5/11E608c - /usr/bin/xcodebuild
    npmPackages:
      expo: ~37.0.3 => 37.0.12 
      react: ~16.9.0 => 16.9.0 
      react-dom: ~16.9.0 => 16.9.0 
      react-native: https://github.com/expo/react-native/archive/sdk-37.0.1.tar.gz => 0.61.4 
      react-native-web: ~0.11.7 => 0.11.7 
    npmGlobalPackages:
      expo-cli: 3.21.12

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
neikercommented, Jul 19, 2020

To anyone reading this:

You can wrap a FlatList (imported from react-native) using PanGestureHandler as described here: https://github.com/software-mansion/react-native-gesture-handler/issues/492

import { FlatList } from "react-native";
import {
  PanGestureHandler,
  NativeViewGestureHandler,
} from "react-native-gesture-handler";

<PanGestureHandler
  ref={panRef}
  simultaneousHandlers={listRef}
  onGestureEvent={gestureHandler}
>
  <NativeViewGestureHandler ref={listRef} simultaneousHandlers={panRef}>
    <FlatList
      keyExtractor={keyExtractor}
      data={data}
      renderItem={renderItem}
    />
  </NativeViewGestureHandler>
</PanGestureHandler>

But, this issue stills valid. Types are wrong, FlatList by react-native-gesture-handler don’t implements either onHandlerStateChange nor onGestureEvent.

2reactions
ArtemKosiakevychcommented, Apr 29, 2021

To anyone reading this:

You can wrap a FlatList (imported from react-native) using PanGestureHandler as described here: #492

import { FlatList } from "react-native";
import {
  PanGestureHandler,
  NativeViewGestureHandler,
} from "react-native-gesture-handler";

<PanGestureHandler
  ref={panRef}
  simultaneousHandlers={listRef}
  onGestureEvent={gestureHandler}
>
  <NativeViewGestureHandler ref={listRef} simultaneousHandlers={panRef}>
    <FlatList
      keyExtractor={keyExtractor}
      data={data}
      renderItem={renderItem}
    />
  </NativeViewGestureHandler>
</PanGestureHandler>

But, this issue stills valid. Types are wrong, FlatList by react-native-gesture-handler don’t implements either onHandlerStateChange nor onGestureEvent.

Note that FlatList must be a child of NativeViewGestureHandler otherwise, it won’t work

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native Gesture Handler + Reanimated Flat List Scroll
I'm having a problem with react-native-gesture handler animation in a FlatList. when i try to scroll on the flatList the behavior of ...
Read more >
React Native Gesture Handler: Swipe, long-press, and more
Implementing gestures in a React Native app improves the user experience. Learn how to create swipeable, pan, long-press, and other ...
Read more >
About Gesture Handlers | React Native ... - Software Mansion
Gesture handler components do not instantiate a native view in the view ... Keep in mind that onGestureEvent is only generated in continuous...
Read more >
Troubleshooting | React Native Bottom Sheet - GitHub Pages
Adding horizontal FlatList or ScrollView is not working properly on Android​. Due to wrapping the content and handle with TapGestureHandler & PanGestureHandler ...
Read more >
react-native-gesture-handler is not updating value after ...
Coding example for the question react-native-gesture-handler is not updating value after movement ... <PanGestureHandler onGestureEvent={gestureHandler} ...
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