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.

initialScrollIndex does not work all the time

See original GitHub issue

Is this a bug report, a feature request, or a question?

This is a bug report.

The possibility to be able to use the props getItemLayout & initialScrollIndex (which I believe is a very good idea because of the optimisation of getItemLayout) does not work all the time.

Sometimes, the initialScrollIndex() does not work and renders the first item. The bug does not appear all the time, sometimes it works perfectly and sometimes it doesn’t. I have tried with a normal Flatlist in which all the items are the size of the screen to be sure if it is not a bug from Flatlist but it never failed. So the problem comes from SnapCarousel.

To explain what I am doing, I use the snap-carousel to show pictures fullscreen and the user can swipe between the pictures. The first picture rendered depends on what picture the user pressed before in a kind of gallery. I always tried with the last item of the list (9 items), and sometimes it show me indeed the last item and sometimes the first item. I do exactly the same actions each time.

I tried also with initialNumToRender() with a value of the index of the first item to show (it was necessary with firstIndex prop) but it does not change anything.

Have you followed the required steps before opening a bug report?

(Check the step you’ve followed - put an x character between the square brackets ([]).)

Have you made sure that it wasn’t a React Native bug?

Yes, because it works perfectly with a normal Flatlist all the time.

Is the bug specific to iOS or Android? Or can it be reproduced on both platforms?

Tried on two android devices : android 8.1 android 9 => both has the problem

Is the bug reproductible in a production environment (not a debug one)?

I tried without the debug on. The problem appears.

Environment

Environment: React: 16.8.6 React native: 0.60.5 react-native-snap-carousel: 3.8.4

Target Platform: Android (8.0 / 0.9) iOS (latest)

Expected Behavior

I expect that initialScrollIndex() show me the index I passed

Actual Behavior

initialScrollIndex() does not show me all the time the index passed and show me the first item of the list.

Reproducible Demo

I will try to post a reproductible demo. But maybe some other people has encountered the same problem so this post is for that.

Steps to Reproduce

Code used:

getItemLayout = (data, index) => {
    return { length: SCREEN_WIDTH, offset: SCREEN_WIDTH * index, index };
}

render() {
    <Carousel
      ref={this.refCarousel}
      data={this.state.render_list}
      renderItem={this.renderItem}
      sliderWidth={SCREEN_WIDTH}
      itemWidth={SCREEN_WIDTH}
      containerCustomStyle={[styles.CarouselStyle]}
      onBeforeSnapToItem={this.onBeforeSnapToItem}
      initialScrollIndex={this.state.initial_index}
      contentContainerCustomStyle={styles.contentContainerCustomStyle}
      removeClippedSubviews
      windowSize={10}
      scrollEventThrottle={16}
      getItemLayout={this.getItemLayout}
      keyExtractor={this.keyExtractor}
      shouldOptimizeUpdates={false}
   />
);

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:7
  • Comments:15 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
cesar3030commented, Nov 7, 2019

Same problem as well. I downgraded to 3.8.1 and it seems to work properly…

0reactions
dohooocommented, Oct 8, 2021

Sorry, please allow me to advertise for my open source library! ~ I think this library react-native-reanimated-carousel will solve your problem. It is a high performance and very simple component, complete with React-Native reanimated 2

Read more comments on GitHub >

github_iconTop Results From Across the Web

initialScrollIndex does not work all the time · Issue #615 - GitHub
Sometimes, the initialScrollIndex() does not work and renders the first item. The bug does not appear all the time, sometimes it works ......
Read more >
initialScrollIndex not working for FlatList react native
I've solved this with setting: onContentSizeChange which will handle scroll to index whenever flatlist data is changed and all items rendered ( ...
Read more >
FlatList jumps to initialScrollIndex on loading. Any ideas why ...
What's the question exactly? It's doing exactly what's expected, it initially scrolls to the 4th index. What are you wanting to happen?
Read more >
initialScrollIndex not working for FlatList react native-React ...
More Query from same tag. I tried to run React Native app for the first time and an Error across me. undefined is...
Read more >
ScrollView - React Native
Creating JS components and native views for everything all at once, much of which may not even be shown, will contribute to slow...
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