react-native-snap-carousel icon indicating copy to clipboard operation
react-native-snap-carousel copied to clipboard

snapToItem/snapToNext not working on Android devices when called from callbacks inside step

Open kgorol opened this issue 5 years ago • 46 comments

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

Bug report

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

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

Yes

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

Android only.

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

I think so.

Environment

Expo with v33 SDK

Expected Behavior

It should go to specified page or to the next.

Actual Behavior

On iOS device it is working fine, but on Android nothing happens.

Reproducible Demo

https://snack.expo.io/SJxF4GwlB

Steps to Reproduce

  1. Run on Android
  2. Try to snapToItem from inside single page view.

kgorol avatar Jul 01 '19 05:07 kgorol

Hi @kgorol,

Can you tell me if this works properly with version 3.7.5 of the plugin?

bd-arc avatar Jul 01 '19 08:07 bd-arc

Thanks for replying @bd-arc,

Works like a charm on 3.7.5 :)

kgorol avatar Jul 01 '19 08:07 kgorol

Still it's not working for me

sansetty2510 avatar Jul 02 '19 10:07 sansetty2510

setTimeout(() => this.carouselComponent.snapToNext(), 250)

adding timeout in the current version works for me :) less than 250 will not snap to next

calz10 avatar Jul 04 '19 04:07 calz10

setTimeout(() => this.carouselComponent.snapToNext(), 250)

adding timeout in the current version works for me :) less than 250 will not snap to next

works for me! thanks :)

pedro-lb avatar Jul 05 '19 01:07 pedro-lb

setTimeout(() => this.carouselComponent.snapToNext(), 250)

adding timeout in the current version works for me :) less than 250 will not snap to next

Works for me too! Thanks!

tannguyen16 avatar Jul 10 '19 19:07 tannguyen16

I think we shouldn't let this end here... We have just one workaround, but nothing concrete yet. Any update coming @bd-arc?

Inovassist-dev avatar Jul 16 '19 19:07 Inovassist-dev

@Inovassist-dev I definitely agree that we need to get to the root of it.

The problem is that I need to know first and foremost if this is a bug introduced in 3.8.0 or something else. Hence my initial question.

But so far I've received contradictory answers...

bd-arc avatar Jul 17 '19 05:07 bd-arc

Yeah, I understand... Maybe I can help you! I'll check last versions in order to see if it's a problem in the latest one! Hope this helps!

Inovassist-dev avatar Jul 17 '19 11:07 Inovassist-dev

Have the same issue running on an iOS device: snapToItem(), snapToPrev(), and snapToNext() don't work. I spent a long time trying to debug on 3.8.0 with no success, except for @calz10's workaround (which is suboptimal since it adds a noticeable delay). @bd-arc, downgrading to 3.7.5 does not fix the problem for me. Also not after resetting all caches. What else can I try? How can we get this to work again?

abegehr avatar Jul 28 '19 12:07 abegehr

The problem is here. When using snapToNext() or snapToPrev(), _snapToItem is triggered again with the old index

QQizi avatar Aug 01 '19 13:08 QQizi

Also not working for me on versions 3.2 - 3.8, adding timeout does work, but the fix is far too slow and hacky to be used. Would like to know how this could be fixed as well.

daaashleywch avatar Aug 01 '19 21:08 daaashleywch

@QQizi any thoughts on how to fix?

daaashleywch avatar Aug 01 '19 21:08 daaashleywch

@QQizi @kgorol Actually got this to work by removing only iOS check for momentum bug fix here Now click through and swipe work for me on Android.

daaashleywch avatar Aug 02 '19 20:08 daaashleywch

@bd-arc I was using version at commit 85980e5177a75d393a1b38480800753282148c50 until today and did not have this problem. I just updated to latest master version (commit 1aebb185036813b1104ea01fc6229b43f10f4d9c) and I am observing this problem. So yes, there is a regression between those two commits.

zabojad avatar Aug 09 '19 08:08 zabojad

+1

sultan-arshi avatar Sep 17 '19 21:09 sultan-arshi

I encounter the same problem on Android (not tested on IOS) I am using react-native 0.60.5 and 3.8.1 (I also tried with 3.7.5 and it still didn't worked)

However, it was working with exactly the same code on react-native 0.59.4 and 3.7.5. So it seems the upgrade of react-native version 0.59.4 to 0.60.5 could be linked.

ALexandreM75013 avatar Sep 24 '19 22:09 ALexandreM75013

@AlexandreMaistret Thank you for the helpful feedback!

Unfortunately your conclusion is the absolute worst case scenario... And I don't see anything that is supposed to affect ScrollView and FlatList in version 0.60's release notes...

bd-arc avatar Sep 25 '19 07:09 bd-arc

@bd-arc I know it is quite a really bad scenario because I looked also the releases notes of 0.60.5 and nothing seems to possibly interfere with thoses functions. I just tried again just now to be sure because I was also very surprised, and same situation : works on 0.59.4 and and not with 0.60.5 with exactly the same code and both 3.7.5.

The workaround with setTimeout works thought, but it is not usable since you can feel the delay and it feels really laggy then.

Just to give you more details on the code: I am calling snapToNext() or snapToPrev() directly from the item with TouchableWithoutFeedback. The touchable event is called and go without any problem to snapToNext() function (I checked with logs) but then nothing, it does not go to the next item. Also it does not work with snapToItem() too. I tried with and without Debug JS activated, and also with JS Dev Mode off.

I didn't tried with iOS since I don't have a Mac.

ALexandreM75013 avatar Sep 25 '19 20:09 ALexandreM75013

In iOS 12.4.1 with React Native 0.60.5, 3.7.5 and 3.8.1 versions work fine, also in production. I will test with React Native 0.59.4 soon, but I think that will work well.

sytabaresa avatar Oct 01 '19 23:10 sytabaresa

"react-native-snap-carousel": "^3.7.5",
"react-native": "0.61.2",

requestAnimationFrame(() => this.carouselRef.snapToPrev()); requestAnimationFrame(() => this.carouselRef.snapToNext());

Is a work around if you're sceptical when upgrading dependancies.

francisleigh avatar Oct 09 '19 16:10 francisleigh

This workaround setTimeout(() => this.carousel.snapToPrev(), 0) is still needed in "react-native-snap-carousel": "^3.8.2", :(

nathantqn avatar Oct 22 '19 03:10 nathantqn

setTimeout(() => this.carouselComponent.snapToNext(), 250)

adding timeout in the current version works for me :) less than 250 will not snap to next

You just saved the day. I almost forgot about this javascript context trick that is very usefull sometimes... thank you!

almeidacavalcante avatar Nov 16 '19 04:11 almeidacavalcante

The only workaround that works for me is enabling momentum by setting props enableMomentum. I suspect that without this props, the _activeItem is not updated when calling snapToNext/snapToPrev.

source code

appli-intramuros avatar Nov 26 '19 13:11 appli-intramuros

This solution setTimeout(() => this._carousel.snapToNext(), 0) and this one requestAnimationFrame(() => this._carousel.snapToPrev()) work for me, however the animation disappears in both cases (only on android). Any hint for this problem?

carmenchapa avatar Dec 11 '19 10:12 carmenchapa

This solution setTimeout(() => this._carousel.snapToNext(), 0) and this one requestAnimationFrame(() => this._carousel.snapToPrev()) work for me, however the animation disappears in both cases (only on android). Any hint for this problem?

The same, the animation disappears in both cases (only on android).

valeriik avatar Dec 24 '19 10:12 valeriik

I'm facing the exact same problem with @valeriik and @carmenchapa. I tried to use enableMomentum, but it makes the animation on Android looks a bit weird

nathantqn avatar Dec 27 '19 04:12 nathantqn

@9600baud this thread documents the issue we are seeing on android.

nlokare avatar Jan 27 '20 20:01 nlokare

Just for a little further information (on the most recent 3.8.4 version): snapToNext() works if used outside of the carousel like this:

<Fragment>
  <Carousel
    ref={(c) => { this.carousel = c }}
    data={data}
    ...
  />
  <TouchableOpacity 
    style={{ backgroundColor: 'lime', position: 'absolute', bottom: 24, width: '90%', height: 42 }} 
    onPress={this.carousel.snapToNext} />
</Fragment>

But if you call this.carousel.snapToNext() from a carousel item it does not work (and must use the workarounds mentioned by others on this thread):

renderCarouselItem = ({ item, index }) => {
  return <SectionInput
    saveSection={() => { this.carousel.snapToNext(); this.saveSection() }}
    index={index}
    section={item}/>
}

bfaulk96 avatar Feb 10 '20 16:02 bfaulk96

Update: Pull Request #648 fixes this! Are there any maintainers (@bd-arc maybe?) that can take a look to get this fix out? It appears that the issue was, when triggering the snapToNext() or snapToPrev() based on a button inside the carousel, the touch event was being seen the same as a touch & drag event, which triggered the snapToItem() function. The PR mentioned above differentiates the two, which fixes our problem

bfaulk96 avatar Feb 10 '20 17:02 bfaulk96