React Native: Optimized SectionList does not rerender after scrollToLocation - react-native

I built an alphabet scrubber that scrolls a SectionList. The SectionList is optimized through the use of getItemLayout. If I scrub over the alphabet, the scrolling happens as planned, but the SectionList does not rerender until I release my finger from the screen.
See this expo snack
Anyone ideas on how to solve this?

Solved it by going through the source code of PanResponder. PanResponder sets an interaction handle through the InteractionManager. You can access this handle through this.panResponder.getInteractionHandle() (undocumented feature), and then clear it everytime you scroll to a location in the SectionList:
if (this.panResponder.getInteractionHandle()) {
InteractionManager.clearInteractionHandle(
this.panResponder.getInteractionHandle()
);
}
This will move the SectionList up in the queue for doing its visibility calculations and renders.
I advice to heavily optimize this by clearing the interaction handle as few times as possible, since the InteractionManager is used for performance reasons.

For the sectionList to update dynamically you need to place the data its using into State. This Expo example(done by someone else), is a good example:
https://snack.expo.io/Syy1bqKxW
His panResponder has all its data in state and on every "handlePanResponderMove" he calls setState() to update the position every time it moves. All in all, if your data is not in state, it will not update dynamically for you. Placing your data into state in React-Native is how it keeps track of your data changes dynamically.
Hope this helps!

Related

Animating FlatList items

In my application I want to create a couple animations on my FlatList.
For example, after fetching the data and feeding it to the list I want the items that should be visible to slide from the left. When I'm scrolling each item (at the top) that supposed to disappear will slide out to the right and each item that should appear (at the bottom) should slide from the left. Is it possible in React Native?
I only managed to create one type of animation - items sliding right when they are about to disappear, but I don't have any idea how to make the items to appear from the left.
My Animated.View can receive only one type of transform. So how can create different types of input/output ranges for the top and the bottom of the list?
I tried to find some examples on the internet but couldn't find any for multiple animations, only for one type.
I think you will find React Native Reanimated's Entering and Exiting Animations API
useful for this task. It greatly simplifies animations like this in my experience.
In case you want to get more control. Using a FlatList, you can also use its onScroll prop to get the current value for YOffset (contentOffset.y) via the Reanimated useAnimatedScrollHandler. Thus you can figure out how much has been scrolled.
Which you could then use to manually apply any translateX's required to the Animated.View of whatever items the flatlist is rendering. The logic you'll of course have to figure out though. But it's a start.
Be sure to do any animation interpolates using UI thread worklets on Reanimated only. Good luck!

(iOS) Is it possible to move loading spinner in FlatList refreshing prop?

I am trying to use pull to refresh from FlatList. Is it possible to move the spinner to where the data actually starts rendering instead of the top of the screen? I found the progressViewOffset that works on Android but I am trying to find how I can implement this behaviour on iOS too. I feel like my solution is to watch scroll interactions and go on from there.
Any idea is appreciated. Thanks in advance.

Instagram Feeds on React Native

I was researching this since last year from time to time. I always wonder how Instagram handles the feeds screen?
What kind a component they using for it?
I'm sure it's not a ScrollView becouse it's much more performant than a ScrollView.
It is not also a FlatList becouse I know the basics of the FlatList how it renders the rows etc... and I am sure that they don't use it. For example a FlatList renders the items on top of each other like a card stack. In this behavior you cannot achieve the current Pinch Zoom feature that they have.
I thought maybe they use flipkart's RecyclerListView which is based on Java and much more performant than ScrollView and FlatList, but I am not quite sure about that too.
Any ideas about what they using to achieve current functionality on the feeds screen?
I mean, is there a method or specific listing component to achieve same behavior with all the functions like Double Tap to Like, like Pinch Zoom with overlaying everything.
#Quick note about pinch zooming, it's nearly impossible to build a component with current states of ScrollView and FlatList which will be performant and has same functionality.
You can check out my other topic about that here
Long story short, I have tried a lot of methods and 3rd party components to achieve same thing, but still could not do it.
Thanks for your thoughts!

React Native: ScrollView with auto scroll

I would like to create a carousel that scrolls automatically until the user scrolls / touches the ScrollView itself.
The auto-scrolling itself works fine with using scrollView.scrollTo but how could I detect if the user is interacting with the ScrollView? I took a look at the onScroll event but this does not seem to distinct between a user generated event and an event that was generated by calling scrollTo.
Also I'd like to know if it is possible to get the current scroll position from the ScrollView directly instead of reading it everytime from the onScroll event.
I'm very thankful for any tips and suggestions.
By digging into ScrollView's source code you can notice a few undocumented callbacks that will help you achieve what you're after, namely onTouchStart and onTouchEnd. These two callbacks are triggered only when user interacts with the ScrollView and not when you scroll programmatically.
You will probably want to clear your auto-scroll interval on onTouchStart and restart it after a delay on onTouchEnd.
Regarding your next question, the answer is no. As far as I know, no getter is currently exposed to retrieve the current scroll position. Therefore, you need to rely on the event passed to onScroll, retrieve event.nativeEvent.contentOffset['x' or 'y'], and store it in your component's state.
Note that if you're doing some heavy animations that need to follow scroll position closely (e.g. animated header or parallax image), it would be a good idea to use the native driver for Animated.event. You can learn more about it on React Native's blog.

In react native is a List or a ScrollView better?

I'm working on a chat like application and I have seen examples using ListView and ScrollView. What are the advantages of either? I need to render different looking items through out the chat (inputs vs. responses OR text vs. images). Does one handle this case better?
I think your question is "ScrollView vs FlatList - which one to use"?
According to React-Native docs:
ScrollView vs FlatList - which one to use?
ScrollView simply renders all its react child components at once. That makes it very easy to understand and use.
On the other hand, this has a performance downside. Imagine you have a very long list of items you want to display, maybe several screens worth of content. Creating JS components and native views for everythign all at once, much of which may not even be shown, will contribute to slow rendering and increased memory usage.
This is where FlatList comes into play. FlatList renders items lazily, just when they are about to appear, and removes items that scroll way off screen to save memory and processing time.
FlatList is also handy if you want to render separators between your items, multiple columns, infinite scroll loading, or any number of other features it supports out of the box.
I would use FlatList. This is what u need. It's more effective & lazy loads your data only when needed.