React-native-swiper: Is there any way to get current screen element?

Created on 9 Sep 2019  路  6Comments  路  Source: leecade/react-native-swiper

Hello,
Is there any way to get current screen element?

By index is not possible, because I update the array of elements dynamically and the index is getting broken when swipe between elements?

Thank you.

Most helpful comment

@ochikov My current workaround is to use onIndexChanged and save it to a state value, which I can then use:

<Swiper
  ...
  onIndexChanged={index => this.setState({ swiperIndex: index })}
  ...>
  ...
</Swiper>

All 6 comments

Having the ability to grab the current index would be amazing; I too can't find a way to do this.

@ochikov My current workaround is to use onIndexChanged and save it to a state value, which I can then use:

<Swiper
  ...
  onIndexChanged={index => this.setState({ swiperIndex: index })}
  ...>
  ...
</Swiper>

@sunweiyang Can you describe how this works? I want to use the current index to do something but I'm not sure where that number is being stored using the code above.

@devinjameson Once onIndexChanged passes the index value to a component state (say we named it swiperIndex, you can access it using this.state.swiperIndex. For example:

export default YourComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      swiperIndex: 0
    };
  ...
  render() {
    console.log("Your current swiper index is: " + this.state.swiperIndex")
    return (
      <Swiper
        ...
        onIndexChanged={index => this.setState({ swiperIndex: index })}
        ...>
        ...
      </Swiper>
    )
  }
}

Oh I see thank you @sunweiyang!

In 1.5.14, it is this.swiper.current.state.index,
In 1.6.0, it as this.swiper.state.index.

To see all possible methods, just log "this.swiper" to console. I don't know the reason, but some methods are missing in documents.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AndrewSouthpaw picture AndrewSouthpaw  路  3Comments

Liqiankun picture Liqiankun  路  3Comments

gwhite-dayspring picture gwhite-dayspring  路  3Comments

nomoreboredom picture nomoreboredom  路  3Comments

agzuniverse picture agzuniverse  路  3Comments