machadogj / react-native-carousel-control

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

button to change view #18

Closed ncampos92 closed 7 years ago

ncampos92 commented 7 years ago

I want to put a button in the carousel to go to the next or previous view, is this possible?

machadogj commented 7 years ago

Hi @ncampos92, Yes you can. You can use any Touchable component, and im the onPress call the "goToPage" method. For calculating the next/previous item, you can use the "onPageChange" to update the index. you can wrap the component in a view, and then use position absolute to place your buttons wherever you want. Let me know if that helps.

ncampos92 commented 7 years ago

Hi @machadogj im trying to call the goToPage method but im getting an error. I call "Carousel.goToPage(index)" and get "undefined is not a function". Can you help me with this?

machadogj commented 7 years ago

Hi @ncampos92 you actually need to get a hold of the reference first. Something like this in your render method:

render() {
  return (
    <View.....>
      <Carousel ref={ c => this.carousel = c } .... >....</Carousel>
    </View>
  );
}

And then you can do:

handleOnNext() {
  this.carousel.goToPage(3); //whatever you need here.
}
ncampos92 commented 7 years ago

Thanks @machadogj now i got it to work!

machadogj commented 7 years ago

Sounds good!