React-native-swiper: add scrollTo function

Created on 26 Aug 2016  ·  1Comment  ·  Source: leecade/react-native-swiper

Hello it will be really nice to have a scrollTo function in Swiper, in my case i want to scroll to view 5 from view 1 and viceversa.

Thanks a lot!

Most helpful comment

Since it looks like this package isn't getting updated soon... Here's how to implement a hacky "scrollTo" method

// Listen for swiper index changes
let swiperIndex = 0
Swiper.prototype.componentWillUpdate = (nextProps, nextState) => {
  swiperIndex = nextState.index
}

class ... {
    swiper: Object

    render() {
        <Swiper ref={(ref) => {this.swiper = ref}}>
           ...
        </Swiper>   

        <Something onPress={() => {
              // Scroll back to a specific index (you may require better logic)
              const targetIndex = 0
              this.swiper.scrollBy(targetIndex - swiperIndex, true)
        }} />
    }
}

>All comments

Since it looks like this package isn't getting updated soon... Here's how to implement a hacky "scrollTo" method

// Listen for swiper index changes
let swiperIndex = 0
Swiper.prototype.componentWillUpdate = (nextProps, nextState) => {
  swiperIndex = nextState.index
}

class ... {
    swiper: Object

    render() {
        <Swiper ref={(ref) => {this.swiper = ref}}>
           ...
        </Swiper>   

        <Something onPress={() => {
              // Scroll back to a specific index (you may require better logic)
              const targetIndex = 0
              this.swiper.scrollBy(targetIndex - swiperIndex, true)
        }} />
    }
}
Was this page helpful?
0 / 5 - 0 ratings