React-native-swiper: Programatically change indexes?

Created on 30 Sep 2017  路  4Comments  路  Source: leecade/react-native-swiper

Is there a way of changing the index programatically? Say by stating the currently viewed 'slide' as a property of the state: state = { currentSelectedSlide: 1 }?

I tried doing Swiper.scrollBy(1); and got an error scrollBy is not a function.

Most helpful comment

Just wanted to leave an update here for people using React-Native with hooks and functional components. You need to use "current" to access the scrollBy function.

const swiper = useRef(null)

<Swiper ref={swiper}>
  <View>
    <Button onPress={() => swiper.current.scrollBy(1)} title="Page 1" />
  </View>
  <View>
    <Button onPress={() => swiper.current.scrollBy(1)} title="Page 2" />
  </View>
</Swiper>

All 4 comments

Are you correctly assigning the ref? I'm using this.swiper.scrollBy(1); and it's working as intended.

@ttargo1 throws swiper is not a function

@mitajunior make sure you're assigning swiper as the swiper's ref.

js <Swiper ref={(swiper) => {this.swiper = swiper;}} >

Just wanted to leave an update here for people using React-Native with hooks and functional components. You need to use "current" to access the scrollBy function.

const swiper = useRef(null)

<Swiper ref={swiper}>
  <View>
    <Button onPress={() => swiper.current.scrollBy(1)} title="Page 1" />
  </View>
  <View>
    <Button onPress={() => swiper.current.scrollBy(1)} title="Page 2" />
  </View>
</Swiper>
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

AndriiBoiko picture AndriiBoiko  路  3Comments

itinance picture itinance  路  3Comments

Liqiankun picture Liqiankun  路  3Comments

diegolmello picture diegolmello  路  3Comments