machadogj / react-native-carousel-control

React Native Carousel control with support for iOS and Android
MIT License
247 stars 55 forks source link

How can I get the actual page from the onPageChange event? #9

Closed sergioribeiro closed 8 years ago

sergioribeiro commented 8 years ago

<Carousel onPageChange={ self.myFunction() }> { array } </Carousel>

machadogj commented 8 years ago

Hi @sergioribeiro the onPageChange returns the index of the page, and you should have the array you passed to the carousel to retrieve the page. Is that what you mean? What do you need it for?

sergioribeiro commented 8 years ago

I need to know when the user slides to the last page.

machadogj commented 8 years ago

@sergioribeiro you can know that with the index passed in onPageChange. See this sample:

  state = {
    words: [
      'foo',
      'bar'
    ]
  };

  handleOnPageChange(pageIndex) {
    if (pageIndex === this.state.words.length - 1) {
      console.log('we reached the end!');
    }
  }

  render() {
    const pages  = this.state.words.map(w => <Text key={w}>{w}</Text>);
    return (
      <View style={styles.container}>
        <Carousel
          initialPage={ 1 }
          pageStyle={ {backgroundColor: 'white', borderRadius: 5} }
          onPageChange={ this.handleOnPageChange.bind(this) }
        >
          { pages }
        </Carousel>        
      </View>
    );
  }

Let me know if that helps.

sergioribeiro commented 8 years ago

Thank you very much!

machadogj commented 8 years ago

Welcome!