react-native-swiper icon indicating copy to clipboard operation
react-native-swiper copied to clipboard

Update the state breaks the slides.

Open ochikov opened this issue 6 years ago • 35 comments

Which OS ?

MAC OS

Version

Which versions are you using: ^1.6.0-nightly.3

  • react-native-swiper v?
  • react-native v0.59.5

Hello Guys, I am using Swiper like this:

renderThumb() {
        return (
            <Swiper style={styles.wrapper} showsButtons={false} horizontal={false} showsPagination={false} loop={false} onIndexChanged={this.onSlideIndexUpdate} onMomentumScrollEnd={() => { console.log('on touch end') }}>
                {this.state.cachedThumbnails.map((thumb) => {
                    console.log('HERE THUMB', thumb)
                    return (
                        <View key={thumb.key} style={styles.thumbnailContainer}>
                            <Loader loading={this.state.showThumbnail} withBackground={false} showLoadingText={false} />
                            <Image
                                source={{ uri: thumb.thumbLink }}
                                style={styles.backgroundVideo}
                                resizeMode='cover'
                            />
                  
                        </View>
                    );
                })}
            </Swiper>
        );
    };

Every time when the state is changed, the component is re-rendered, but the slide is pushed up and the next slide is seeing from the bottom, as showed here in the image:

Please note, this happens only on the images with index>0. If I swipe to the first slide, the slide is ok.

Screen Shot 2019-09-05 at 16 39 46

Screen Shot 2019-09-05 at 16 39 55

ochikov avatar Sep 05 '19 13:09 ochikov

I'm experiencing something very similar. Everything works great until I add onIndexChanged.

My code looks something like this:

function (props: Props) {
  const questions = idx(props, _ => _.viewer.findSurvey.questions) || [];
  const name = idx(props, _ => _.viewer.findSurvey.name);
  const [page, setCurrentPage] = React.useState(0)
  console.log({ page })

  return (
    <Shell title={name}>
      <Swiper
        showsVerticalScrollIndicator={true}
        horizontal={false}
        loop={false}
        showsPagination={false}
        onIndexChanged={page => setCurrentPage(page)}
      >
        {questions.map(item => {
          return (
			<View ...

Using Expo and the latest version of this library.

Happy to share more if it's helpful!

hanford avatar Sep 06 '19 17:09 hanford

Thank you. I am going to test it and report back to you later today.

On Fri, 6 Sep 2019 at 20:39, Jack Hanford [email protected] wrote:

@ochikov https://github.com/ochikov I went back to version 1.5.6 and this stopped happening

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/leecade/react-native-swiper/issues/1053?email_source=notifications&email_token=ADHVGEVIE6KEZVA4OU5NYR3QIKIVJA5CNFSM4IT6ACL2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6DRK6Y#issuecomment-528946555, or mute the thread https://github.com/notifications/unsubscribe-auth/ADHVGERFHQWSG46KTGA64GDQIKIVJANCNFSM4IT6ACLQ .

ochikov avatar Sep 07 '19 06:09 ochikov

@ochikov let me know what you find, i’m planning on potentially forking and removing all of the features i don’t need to try and narrow down the problem

hanford avatar Sep 07 '19 16:09 hanford

@hanford Switching the versions fix the problem!

ochikov avatar Sep 09 '19 07:09 ochikov

@ochikov which version are you using?

hanford avatar Sep 09 '19 16:09 hanford

@hanford I have downgrade to 1.5.6

ochikov avatar Sep 09 '19 18:09 ochikov

I'm using a heavily modified version of the plugin, but if it's helpful here it is: https://gist.github.com/hanford/973d7d2a4bab09b1e559fac6dfae9c5d .. I removed a lot of code, looping, autoplay, etc

I think one of the big differences is this specific modification: https://gist.github.com/hanford/973d7d2a4bab09b1e559fac6dfae9c5d#file-swiper-js-L418

hanford avatar Sep 09 '19 18:09 hanford

Having worked with various versions of this Swiper over the last month, I can confirm that there is definitely something not right with the very latest versions and I've had to settle on v. 1.5.14. The issue seems to be around the new 'scrollTo' methods that get in a muddle when updating the slides with dynamic content or changing their length on the fly.

Occasionally, when updating the state of the page (and causing a re-render), the 'onIndexChanged' is getting fired off, and reports an index of -1. Any attempt to update state within the callback triggers it to fire off yet again and go to -2 / -3 ad infinitum.

I think this new 'scrollTo' thing needs a lot more serious thought and testing, as it seems too unreliable for my liking at present.

edanedison avatar Oct 02 '19 12:10 edanedison

@edanedison, I also think a lot more testing is necessary, so I add e2e test now 🤔. Feel free to add the bad cases for you under https://github.com/leecade/react-native-swiper/tree/develop/examples/e2e.

ArrayZoneYour avatar Oct 03 '19 06:10 ArrayZoneYour

Thanks @ochikov I downgraded to 1.5.5 and it fixed my issue whereby my slides were automatically half swiping (possibly due to state change)

P.S. 1.5.6 wasn't available in the versions list (npm view versions)

samipshah100 avatar Oct 03 '19 09:10 samipshah100

Downgrade to 1.5.5 helped for me too. I had the problem only in iOS and with versions 1.5.13 and 1.5.7. (I guess all versions between are not working either?)

I am using react-native-swiper in two places ("tutorial" and "achievements") in my project. 1.5.13 is working correctly also with iOS in the tutorial but in the achievements this problem occurred. Differences with these use cases are atleast that:

  • Usage is quite static in tutorial. Only fullscreen slides with dots. Achievements use onMomentumScrollEnd to set state of an index.
  • Achievements use also ref and scrollBy-method and it takes only part of the screen and is used inside Modal

henkkasoft avatar Oct 08 '19 10:10 henkkasoft

same issue here

sslash avatar Oct 23 '19 20:10 sslash

Can confirm that setState inside onIndexChanged is one way to cause broken pagination. Downgrading to 1.5.5 caused other errors. Had to remove setState for now and the header dynamic link location functionality until this is fixed.

Enalmada avatar Oct 25 '19 04:10 Enalmada

As it stands the lib seems completely unusable due to this. Building from @hanford 's advice, the following seems to fix the issue on the latest version. https://github.com/kyle-ssg/react-native-swiper/commit/eeddedb4205d28c79853f8d39b2fa5750d254069#diff-1fdf421c05c1140f6d71444ea2b27638

Looks like some internal state values aren't being included when rendering, maybe it could just use this.internals.offset. Added a PR for this.

kyle-ssg avatar Oct 26 '19 12:10 kyle-ssg

I can confirm @kyle-ssg fixes that problem. Has it been PR yet?

zabojad avatar Nov 22 '19 13:11 zabojad

It is in a pr indeed @zabojad https://github.com/leecade/react-native-swiper/pull/1091

kyle-ssg avatar Nov 22 '19 20:11 kyle-ssg

It is in a pr indeed @zabojad #1091

Who can/should do the merging?

mi5ha avatar Nov 29 '19 12:11 mi5ha

This is not merged yet :( ?

Jalson1982 avatar Jan 14 '20 19:01 Jalson1982

tks @kyle-ssg resolved my issue!

dnlsilva avatar Jan 16 '20 20:01 dnlsilva

@kyle-ssg when the solution will be merged?

ochikov avatar Jan 17 '20 09:01 ochikov

If set prop loadMinimal it works in my case.

Jalson1982 avatar Jan 17 '20 10:01 Jalson1982

As it stands the lib seems completely unusable due to this.

@kyle-ssg Hello, which version did your fix solve the problem for? because even now with 1.6.0 the issue still exists on iOS

hshbaklo avatar Apr 14 '20 12:04 hshbaklo

Hi @hshbaklo my PR is still open, so looks like the code was never merged.

kyle-ssg avatar Apr 14 '20 12:04 kyle-ssg

@kyle-ssg yes I know. but since it worked for you, I am trying to understand what versions you had when it worked. if you can share your React , react-native and react-native-swiper you had when it worked for you would be great. just for reference: react-native: 61.5 react:16.9 react-native-swiper:1.6.0 problem on iOS only for us. Note that the swiper is in a view which has right and left padding. when we removed the padding and allowed the swiper to take full screen width, this issue was not solved 100%, but the offset on each slide because almost unnoticeable and only with large number of swipes it can show. So hopefully a permanent fix can be make

hshbaklo avatar Apr 14 '20 13:04 hshbaklo

+1 to @kyle-ssg PR.

I'm using his branch because it fix this problem!

tomahock avatar Apr 27 '20 17:04 tomahock

I'm facing this problem as well. @Jalson1982 solution works for me. Thanks

StevenWonder avatar Sep 15 '20 14:09 StevenWonder

Is there any plan to merge this fix?

k-sav avatar Oct 14 '20 01:10 k-sav

@Jalson1982 works for me

VictorPrata avatar Oct 19 '20 21:10 VictorPrata

Strange, as @Jalson1982 mentionned, loadMinimal={true} seems to workaround the problem...

zabojad avatar Feb 18 '21 10:02 zabojad

Just remove contentOffset={this.state.offset} line in src/index.js.

maacofficial avatar Mar 04 '21 11:03 maacofficial