Skip to content

Instantly share code, notes, and snippets.

@vincicat
Created March 2, 2023 16:04
Show Gist options
  • Save vincicat/beb811b76cef7e646bd5e7845c8bb311 to your computer and use it in GitHub Desktop.
Save vincicat/beb811b76cef7e646bd5e7845c8bb311 to your computer and use it in GitHub Desktop.
ScrollView & FlatList ceveats...

ScrollView

onMomentumScrollEnd

Some mention onMomentumScrollEnd will fire multiple times. To solve it we need a solution that will not trigger re-rendering like Ref or SharedValue (Reanimated 2)

  import { useSharedValue } from 'react-native-reanimated';
  const canMomentum = useSharedValue(false); // or use `useRef` API

solution:

  const onMomentumScrollBegin = ({ nativeEvent }) => {
    canMomentum.value = true;

    console.log(Date.now(), 'onMomentumScrollStart', nativeEvent);
  };

  const onMomentumScrollEnd = ({ nativeEvent }) => {
    if (canMomentum.value) {
      console.log(Date.now(), 'onMomentumScrollEnd', nativeEvent);
      // your stuff
    } else {
      console.log(Date.now(), 'onMomentumScrollEnd dropped', nativeEvent);
    }

    canMomentum.value = false;
  };
  

and try to make the content height as static as possible. using flashlist will not trigger unneccessary re-render.

FlatList

onEndReached

onEndReached will fire once when the end of the list is visible under this two condition:

  • the current list is too short so the end exposed
  • the user scroll to the end of the list (overdrag)

the expected behaviour actually is to fill the list until the end isn't expose

so control your event fire and stop crawling when all data received.

RefreshControl

This one is...complex when you group it with native header searchbar (UISearchController after iOS11 [ref])

read more:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment