Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onIndexChanged handler gets incorrect state #938

Closed
deepakaggarwal7 opened this issue Feb 22, 2019 · 3 comments
Closed

onIndexChanged handler gets incorrect state #938

deepakaggarwal7 opened this issue Feb 22, 2019 · 3 comments

Comments

@deepakaggarwal7
Copy link
Contributor

MAC, running on iOS emulator

  • react-native-swiper": "^1.5.14
  • react-native": "0.57.5

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.

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>
        )
    }
}

ezgif com-resize

@deepakaggarwal7 deepakaggarwal7 changed the title onIndexChanged setstate onIndexChanged handler gets incorrect state Feb 22, 2019
@EvanMaFYH
Copy link

same here

@retyui
Copy link
Contributor

retyui commented Mar 18, 2019

#713

@ArrayZoneYour
Copy link
Collaborator

merged #939. Thanks a lot 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants