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.

FlatList snapToInterval is not precise in long list

See original GitHub issue

Environment

Environment: OS: macOS 10.14 Node: 8.12.0 Yarn: Not Found npm: 6.4.1 Watchman: 4.9.0 Xcode: Not Found Android Studio: 3.2 AI-181.5540.7.32.5014246

Packages: (wanted => installed) react: 16.3.1 => 16.3.1 react-native: https://github.com/expo/react-native/archive/sdk-29.0.0.tar.gz => 0.55.4

Description

I’m using the FlatList component with around 50 elements. When my element height (and subsequently snapToInterval prop) are set to floats, rather than integers, there seems to be a proportionality increasing offset from the correct interval to snap to. The affect of this is very noticeable with a lot of elements. The further I scroll down a list the bigger the offset, so that when I get to the bottom, it’s very noticeable.

This offset is still there with integers, but it’s a lot less pronounced. Screenshots:

First two are with height set to 105.14285714285714, which is an iPhone 6s Plus’ height divided by seven. The last two have height set to 105, which is just the truncated version of the previous height.

Height: 105.14285714285714 (at bottom): 105 14285714285714 at bottom

Height: 105.14285714285714 (second from top): 105 14285714285714 second from top

Height: 105 (at bottom): 105 at bottom

Height: 105 (second from top): 105 second from top

Reproducible Demo

https://snack.expo.io/@elliotschep/flatlist-snaptointerval-is-not-precise-in-long-list

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:4
  • Comments:19 (3 by maintainers)

github_iconTop GitHub Comments

37reactions
ttaallllcommented, Dec 18, 2019

To anyone else facing this: I got around this issue by using snapToOffsets to achieve the same effect.

snapToOffsets solved it for me too.

I just calculated the stops of the next snap by the number of items and the margin between them.

for example:

const { width } = Dimensions.get('window');
this.IMAGE_WIDTH = width * (1 / 2.5)
this.image_margin = 5
this.nishhar = width - ((this.IMAGE_WIDTH + this.image_margin) * 2 + this.image_margin * 2)

dataNum = [1, 2, 3, 4, 5, 6]

return (<FlatList
            data={dataNum}
            renderItem={item => {
                return (
                    <View style={{
                        backgroundColor: item.index % 2 == 0 ? 'red' : 'blue',
                        width: this.IMAGE_WIDTH,
                        height: this.IMAGE_WIDTH,
                        marginLeft: this.image_margin,
                        marginRight: this.image_margin,
                    }}>
                    </View>)
            }}
            keyExtractor={this.keyGenerator}
            horizontal={true}
            snapToAlignment={"start"}
            snapToOffsets={[...Array(dataNum.length)].map((x, i) => (i * (this.IMAGE_WIDTH + 2 * this.image_margin) - (this.nishhar * 0.5)))}
            decelerationRate={"fast"}
            pagingEnabled
        />)
4reactions
mjmasncommented, Oct 9, 2018

There is a similar issue on Android and horizontal ScrollView with devices like the Nexus 5X which apparently has a screen width of 411.428571429dp (wtf?), both with pagingEnabled and with integer intervals you can see the rounding errors creeping up as you scroll further along the view. On devices with integer screen width there is no problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

why is snapToInterval not working for longer arrays in flatlist?
you have to set. snapToInterval. prop to the height of your item "60 ". you don't need getItemLayout if you don't want to...
Read more >
FlatList snapToInterval is not precise in long list - Expo Snack
Try this project on your phone! Use Expo's online editor to make changes and save your own copy.
Read more >
How to add Snap to alignment feature in FlatList in React Native
In React Native, the FlatList component shows similarly structured data in a scrollable list. It is the best option if you have a...
Read more >
ScrollView - React Native
Imagine you have a very long list of items you want to display, ... if snapToInterval is enabled, since it does not follow...
Read more >
Advanced React Native FlatList animations at 60fps with ...
I decided to make another advanced FlatList #ReactNative animation video tutorial this time. This is an animated carousel/animated slider ...
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