Is there any way to get current screen element?
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.
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.
onIndexChanged dosen't work for me :'(
need to set swipe left or right detect how ?
You can get the current index directly on the swiper instance with swiperRef.current._reactInternals.memoizedState.index (swiperRef.current being the swiper instance)