React-native-snap-carousel: snapToItem bug

Created on 26 Mar 2019  路  12Comments  路  Source: meliorence/react-native-snap-carousel

when I use snapToItem(index, false) with X > 3, It was usually return me to my Index then turn back to Index 0. I don't know why?, I don't add any props

contribution? reproduction?

Most helpful comment

If you are facing :
it would snap to 0 slide when you are trying to snap to position x

simply add prop: getItemLayout to calculate offset.

example:

getItemLayout={(data, index) => (
              {length: viewportWidth, offset: viewportWidth * index, index}
)}

All 12 comments

鈿狅笍 You did not follow the contributing guidelines!

As stated in these:

You need to fill out the issue template completely. This step is mandatory! Not doing so will result in your issue getting closed. Don't take this personally if this happens, and feel free to open a new issue once you've gathered all the information required by the template.


Unless you provide a Snack example in which the issue can be reproduced, as well as a complete description of everything you've tried, we won't be able to help you and the thread will be closed.

I have the same issue. @mlop113 were you able to find a solution?

I can confirm that this issue has a bug on the latest RN version 0.59.8

+1

@eggybot @yoavapi Can you tell me if you experience the issue with and without prop loop enabled?

@eggybot @yoavapi Can you tell me if you experience the issue with and without prop loop enabled?

same experience with and without loop.
please note that the Pagination component is being updated correctly (changing the focused dot) but the actual item is not visible.

@bd-arc when we enable loop on the carousel it renders 3 clones, right? (or 6?)

If I press a button which is dynamically calling this._carousel.snapToItem(index) the menu is flickering and not going to the right index.

WHat is going wrong?

@kevinvugts
temp fix with setTimeout, check this link

@kevinvugts
temp fix with setTimeout, check this link

I am only experiencing this when I have loop=enabled. Do you know what's going wrong?

Please have a look: https://www.youtube.com/watch?v=f5b7vzuIzHc&feature=youtu.be

@eggybot @yoavapi Can you tell me if you experience the issue with and without prop loop enabled?

Hi @bd-arc I am experiencing this only with loop=true please see the attached video above.
Here is the video with the working solution (loop is disabled on here): https://www.youtube.com/watch?v=MHzPvyWy9VQ&feature=youtu.be

Code

  renderFiveHundredFilter = ({item, index}) => {
      return (
        <Text onPress={() => {
            setTimeout(() => this._carousel.snapToItem(index), 250)

            this.onPrivilegesRangeChange('none', index)
        }
      } key={`abc_${index}`} style={this._carousel.currentIndex === index ? Styles.activeMenuItem : Styles.menuItem}>{item.name}</Text>
      )
  }

<Carousel
          ref={(c) => { this._carousel = c;}}
          key={'fivehundredlist'}
          data={this.props.items}
          renderItem={this.renderFiveHundredFilter}
          keyExtractor={({name}) => {
            return 'range-button-'+name;
          }}
          sliderWidth={Dimensions.get('window').width}
          itemWidth={100}
          loop={true}
          activeSlideAlignment={'start'}
          activeSlideOffset={2}
          enableMomentum={true}
          onSnapToItem={(slideIndex) => this.onPrivilegesRangeChange('none', slideIndex)}
        />

@bd-arc I found out that this._enableLoop() function in the library is nowhere to be found. Is this on purpose?

Update

I changed the snapToItemFunction to:

snapToItem (index, animated = true, fireCallback = true) {
        const itemsLength = this._getCustomDataLength();
        const { loop, loopClonesPerSide } = this.props;

        if (!index || index < 0) {
            index = 0;
        }

        let positionIndex = this._getPositionIndex(index);
        let newIndex = loop ? positionIndex - loopClonesPerSide : positionIndex;

        if (positionIndex === this._activeItem) {
            return;
        }

        this._snapToItem(newIndex, animated, fireCallback);
    }

And the above code seems to work properly. However it does interfere with the snapToNext() and snapToPrevious methods

If you are facing :
it would snap to 0 slide when you are trying to snap to position x

simply add prop: getItemLayout to calculate offset.

example:

getItemLayout={(data, index) => (
              {length: viewportWidth, offset: viewportWidth * index, index}
)}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

akyker20 picture akyker20  路  3Comments

krisidmisso picture krisidmisso  路  4Comments

duongkhoilinh picture duongkhoilinh  路  4Comments

niloufarMakhzani picture niloufarMakhzani  路  4Comments

radiosilence picture radiosilence  路  4Comments