snapToItem does not work with an index larger than initialNumToRender
Is this a bug report or a feature request?
Bug report
Have you read the guidelines regarding bug report?
Yes.
Have you read the documentation in its entirety?
Yes.
Have you made sure that your issue hasn't already been reported/solved?
I have seen multiple similar issues (#313, #63, #193, #235). I haven't been able to find any duplicates, though. Most of these have ended up being issues with FlatList. However, I don't see that being the case for this issue, as an implementation using FlatList's scrollToOffset shouldn't give any issues. I haven't tested it, though.
Is the bug specific to iOS or Android? Or can it be reproduced on both platforms?
To be honest, I haven't tested it on iOS. I have only tested on Android 8.1.0
Is the bug reproductible in a production environment (not a debug one)?
Yes. I've reproduced it on Android with an app signed and bundled for release.
Have you made sure that it wasn't a React Native bug?
It might be an issue with Flatlist, as setting initialNumToRender and windowSize to large values fixes it.
However, I think an implementation using FlatList's scrollToOffset could fix it.
Have you been able to reproduce the bug in the provided example?
No, I have not reproduced it in the example. I have provided a minimal Snack.
Environment
Environment: React: 16.0.0-beta.5 React native: 0.55.4 react-native-snap-carousel: 3.7.0
Target Platform: Android (8.1.0)
Steps to Reproduce
- Set up a carousel with a large dataset
- Try to use carousel.snapToItem(index) with a large index
- See that it doesn't scroll all the way.
Expected Behavior
Carousel scrolls to the large index.
Actual Behavior
Carousel does not scroll to the large index. It only scrolls to the last item currently rendered.
Reproducible Demo
Have a snack!
Lastly, I just wanted to thank you. Awesome project, guys!
Hi @lukasgjetting,
Thanks for taking the time to put up a top quality bug report!
I ran the ultimate test to find out if this was a FlatList bug: I've set prop useScrollView to true on the carousel. Well, the bad news is that everything works properly when the prop is enabled, which therefore reveals a classic FlatList problem...
Your suggestion of using scrollToOffset is spot on in order to prevent a few FlatList bugs. But note that this has already been implemented a long time ago, based on the insights found in this comment.
Here are a few related threads from the RN repo. Unfortunately, no solution has emerged yet.
- https://github.com/facebook/react-native/issues/1831
- https://github.com/facebook/react-native/issues/8607
- https://github.com/facebook/react-native/issues/13316
- https://github.com/facebook/react-native/issues/13202
- https://github.com/facebook/react-native/issues/15734
- https://github.com/facebook/react-native/issues/16676
- https://github.com/facebook/react-native/issues/19969
I'm still hoping for a proper solution, even though I'm now clearly leaning towards replacing FlatList altogether (see #250 for more info). Do not hesitate to share any info you can find or any workaround you can think of ;-)
with flatlist it works with getItemLayout
this.list!.scrollToIndex({ animated: true, index: lastitem });
<FlatList
horizontal
ref={ref => this.list = ref!}
initialScrollIndex={0}
keyExtractor={(item, i) => i.toString()}
data={this.props.items}
pagingEnabled={true}
showsHorizontalScrollIndicator={false}
onMomentumScrollEnd={this.onSwipe}
renderItem={this.props.renderItem}
onScrollToIndexFailed={crashScrollTo}
getItemLayout={(data, index) => (
{
length: this.props.itemWidth,
offset: this.props.itemWidth * index,
index
}
)}
/>
but with snapcarousel doesn't, it snaps to last preloaded item but not the last item of the list
this.list!.snapToItem(lastitem);
<Carousel
ref={(c) => { this.list = c; }}
data={this.props.items}
renderItem={this.props.renderItem}
sliderWidth={this.props.itemWidth}
itemWidth={this.props.itemWidth}
firstItem={0}
onSnapToItem={this.onSwipe}
/>
@dengue8830 There's a good reason why getItemLayout is no longer used within the Carousel.
See this thread for more info.
Have you found a solution to this problem?
Same problem, why is this thread closed when the issue remains?
Hi @lukasgjetting,
Thanks for taking the time to put up a top quality bug report!
I ran the ultimate test to find out if this was a
FlatListbug: I've set propuseScrollViewtotrueon the carousel. Well, the bad news is that everything works properly when the prop is enabled, which therefore reveals a classicFlatListproblem...Your suggestion of using
scrollToOffsetis spot on in order to prevent a fewFlatListbugs. But note that this has already been implemented a long time ago, based on the insights found in this comment.Here are a few related threads from the RN repo. Unfortunately, no solution has emerged yet.
- facebook/react-native#1831
- facebook/react-native#8607
- facebook/react-native#13316
- facebook/react-native#13202
- facebook/react-native#15734
- facebook/react-native#16676
- facebook/react-native#19969
I'm still hoping for a proper solution, even though I'm now clearly leaning towards replacing
FlatListaltogether (see #250 for more info). Do not hesitate to share any info you can find or any workaround you can think of ;-)
Hi @lukasgjetting,
Thanks for taking the time to put up a top quality bug report!
I ran the ultimate test to find out if this was a
FlatListbug: I've set propuseScrollViewtotrueon the carousel. Well, the bad news is that everything works properly when the prop is enabled, which therefore reveals a classicFlatListproblem...Your suggestion of using
scrollToOffsetis spot on in order to prevent a fewFlatListbugs. But note that this has already been implemented a long time ago, based on the insights found in this comment.Here are a few related threads from the RN repo. Unfortunately, no solution has emerged yet.
- facebook/react-native#1831
- facebook/react-native#8607
- facebook/react-native#13316
- facebook/react-native#13202
- facebook/react-native#15734
- facebook/react-native#16676
- facebook/react-native#19969
I'm still hoping for a proper solution, even though I'm now clearly leaning towards replacing
FlatListaltogether (see #250 for more info). Do not hesitate to share any info you can find or any workaround you can think of ;-)
This workaround is saved my day. Thanks a lot.
This issue just bit me hard for the past week. Now i know why it is happening but do we have a good workaround?
Hi @lukasgjetting,
Thanks for taking the time to put up a top quality bug report!
I ran the ultimate test to find out if this was a
FlatListbug: I've set propuseScrollViewtotrueon the carousel. Well, the bad news is that everything works properly when the prop is enabled, which therefore reveals a classicFlatListproblem...Your suggestion of using
scrollToOffsetis spot on in order to prevent a fewFlatListbugs. But note that this has already been implemented a long time ago, based on the insights found in this comment.Here are a few related threads from the RN repo. Unfortunately, no solution has emerged yet.
- [FlatList] Rows aren't rendered until scroll facebook/react-native#1831
- ListView (or perhaps ScrollView) disappears after running in background for several minutes facebook/react-native#8607
- [FlatList] Sometimes FlatList rows are rendered, but not displayed until scroll. RN 0.43 facebook/react-native#13316
- [FlatList] render issues using
scrollToIndexoncomponentDidMountfacebook/react-native#13202- VirtualizedList + getItemLayout only renders initialNumToRender items facebook/react-native#15734
- [Android][SectionList/FlatList] scrollTo not working because offset is outside rendered "canvas" facebook/react-native#16676
- FlatList scrollToEnd doesn't work unless initialNumToRender is higher than total length? facebook/react-native#19969
I'm still hoping for a proper solution, even though I'm now clearly leaning towards replacing
FlatListaltogether (see #250 for more info). Do not hesitate to share any info you can find or any workaround you can think of ;-)
Thanks, that saved my day as well ;)