meliorence / meliorence/react-native-snap-carousel

Issue with initIndex when adding data to pos 0 of the current dataset.

Open
#629 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
10.5k
Forks
2.3k
PR merge metrics
No merged PRs in 30d

Description

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

This is a bug report/question since I'm not sure if it's just my use case for the library is wrong.

I'll try to provide a sample for the use case that I'm having problem with, and which I have also checked with the code inside of Carousel.js.

  • First the carousel is fetched with a list of eg 4 items: [A, B, C, D], the initIndex will be 0.
  • When the app get a push notification for updating the newest item, the dataset becomes [NewItem1, A, B, C, D] and trigger a render. I want to keep the current screen/card that the user is on, so in this case, I'll set the initIndex to 1 (to continue showing A). This works fine.
  • User now swipes to NewItem1.
  • App got push again, so the data is now [NewItem2, NewItem1, A, B, C, D]. As the app's behavior to keep the current card, the initIndex will still be 1 (to keep NewItem1 that the user swiped to). But inside componentDidUpdate, now the interpolators.length !== itemsLength is true and there is no new hasNewSliderWidth nor hasNewItemWidth, there was no snapping to fix the position to the initIndex. The carousel renders the first item NewItem2 rather than NewItem1.
  • When I try keeping the same amount of data ([NewItem2, NewItem1, A, B]), the check for
    nextFirstItem !== this._previousFirstItem && nextFirstItem !== this._activeItem failed, as at this state the nextFirstItem and this._previousFirstItem are 1.

I could create a setTimeout to make the carousel snap to the new index again, but it would show flickering. So my question is could someone give me some hint on how I could achieve this behaviour without the setTimeout, or should I try to make a patch for this use case?

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 I'm sure it's not a React Native's bug

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

I have only tested on Android, but it should be able to be reproduced on IOS

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

Yes

Environment

Environment:
React: ^16.8.3
React native: ^0.59.8
react-native-snap-carousel: ^3.8.4
Target Platform:
Android (6.0)

Expected Behavior

(Write what you thought would happen.)

Actual Behavior

(Write what happened. Add screencasts/screenshots!)

Reproducible Demo

(Paste the link to a Snack example in which the issue can be reproduced. Please follow the guidelines for providing a Minimal, Complete, and Verifiable example.)

Steps to Reproduce

(Write your steps so that anyone can reproduce the issue in the Snack demo you provided.)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Carousel.js, especially componentDidUpdate and the initIndex handling around interpolators.length, itemsLength, hasNewSliderWidth, hasNewItemWidth, and _previousFirstItem. Reproduce the stated sequence by prepending items while preserving the visible card, then trace why the carousel renders the newly inserted first item. Done means the active card remains stable without a setTimeout and the behavior is covered by a reproducible test or demo.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react-native
Domain
mobile-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.