react-native-swiper: onIndexChanged not called, wrong screen rendered
Version
Which versions are you using:
- react-native-swiper v1.5.8
- react-native v0.47.1
Expected behavior
- onIndexChanged should be called when I swipe
- first View should be rendered as the first screen
Actual behavior
- not called
- third View (with “And simple” message) is rendered as first
Code example
export default class AppIntro extends PureComponent {
onSwipe = (index) => {
console.log('index changed', index);
}
render() {
return (
<Swiper style={styles.wrapper} onIndexChanged={this.onSwipe}>
<View style={styles.slide1}>
<Text style={styles.text}>Hello Swiper</Text>
</View>
<View style={styles.slide2}>
<Text style={styles.text}>Beautiful</Text>
</View>
<View style={styles.slide3}>
<Text style={styles.text}>And simple</Text>
</View>
</Swiper>
);
}
}
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 21
- Comments: 32 (1 by maintainers)
Commits related to this issue
- fix #503, fix a bug where onIndexChanged and onMomentumScrollEnd not firing when load content after init swiper — committed to dktan/react-native-swiper by dktan 7 years ago
I know this is a really old thread, but I was having the same issue and none of the above solutions seemed to work… because it was just me being dumb. 🤦
tldr; you don’t appear to be able to change the number of slides on the fly my and on first render, I had zero.
My problem:
Basically I was creating a Swiper with zero slides, then a second later updating it with multiple slides. All I had to do was wait until I had the data to render the Swiper:
If
onIndexChanged
is not working than try to useGentlemen, still experience the same issue on iOS(android is ok) React 0.48, swiper 1.5.12.
Tried all workarounds…
The last slide is shown instead of first one if loop is enabled. Disabling the loop solving the issue, but…
In
onIndexChanged={(index) => this.setState({index})}
only fires once after the first swipe. The state only changes once when it should be changing each swipe.I solved this (implemented a workaround) by adding a ref to the Swiper and calling
scrollBy
to set the index back to its intended value.And in
componentDidMount
:I am experiencing the same issue.
WORKAROUND / ISSUE FIX FOUND:
NO SUCCESS - included a Height/Width property as indicated above. NO SUCCESS - used onMomentumScrollEnd and no change from onIndexChanged
SUCCESS - added a “key” prop with a unique uuidV4 GUID value and problem WENT AWAY!
Hope this helps
seems this part in updateIndex function gives us this problem:
since this.internals.offset is set to {} in init function not {x:0, y:0}, diff is always NaN and updateIndex always return early.
Seem’s like a dimension issue. Are you giving the height and width to Swiper Component ? Below is the code working for me.
I have the same issue, when debugging it seems like the
internalState.offset
is empty when callingupdateIndex
after a swipe.This causes that diff to beNaN
and that causes the internal index not being updated, which subsequently causesonIndexChanged
not being called.I don’t really know what the internalState.offset should be, so can’t really fix the issue here, but I hope this can help the maintainers to fix the issue
Also experiencing this. I’m seeing onIndexChanged get called after I swipe around a bit.