You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If I call setState in the onIndexChanged handler, it starts passing incorrect index intermittently. Note: If I do not call setState in the handler, it always returns right index. But I wonder why my setState is creating issues
I've also tried other options like onPagingRender or onScrollMomentumEnd etc. but same issue happens in all the moment I use setState in callback handler.
import React, {Component} from 'react'
import { View, Text, StyleSheet, Button } from 'react-native';
import Swiper from 'react-native-swiper';
export default class SwiperDemo extends Component {
constructor(props){
super(props)
this.state = {selectedIndex:0}
}
updateState(index){
console.log('index updated to ' + index);
this.setState({
selectedIndex:index
})};
render(){
let curr=0;
return(
<View style={{flex:1, marginTop:30}}>
<View style={{height:200}}>
<Swiper showsButtons={false}
onIndexChanged={this.updateState.bind(this)}>
<View key={1} style={{height:300,backgroundColor:'red'}}><Text>Index should be 0</Text></View>
<View key={2} style={{height:300,backgroundColor:'yellow'}}><Text>Index should be 1</Text></View>
<View key={3} style={{height:300,backgroundColor:'blue'}}><Text>Index should be 2</Text></View>
</Swiper>
</View>
<Text>Current Index in State = {this.state.selectedIndex}</Text>
</View>
)
}
}
The text was updated successfully, but these errors were encountered:
deepakaggarwal7
changed the title
onIndexChanged setstate
onIndexChanged handler gets incorrect state
Feb 22, 2019
MAC, running on iOS emulator
Expected behaviour
onIndexChanged should return the correct index.
Actual behaviour
If I call setState in the onIndexChanged handler, it starts passing incorrect index intermittently. Note: If I do not call setState in the handler, it always returns right index. But I wonder why my setState is creating issues
I've also tried other options like onPagingRender or onScrollMomentumEnd etc. but same issue happens in all the moment I use setState in callback handler.
The text was updated successfully, but these errors were encountered: