react-native-swiper icon indicating copy to clipboard operation
react-native-swiper copied to clipboard

Is there any way to get current screen element?

Open ochikov opened this issue 6 years ago • 9 comments

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.

ochikov avatar Sep 09 '19 12:09 ochikov

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

sunweiyang avatar Oct 02 '19 22:10 sunweiyang

@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 avatar Oct 02 '19 22:10 sunweiyang

@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 avatar Dec 07 '19 22:12 devinjameson

@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>
    )
  }
}

sunweiyang avatar Dec 09 '19 01:12 sunweiyang

Oh I see thank you @sunweiyang!

devinjameson avatar Dec 09 '19 14:12 devinjameson

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.

yalcinozer avatar Aug 26 '20 07:08 yalcinozer

onIndexChanged dosen't work for me :'(

semahmrad avatar Mar 16 '22 13:03 semahmrad

need to set swipe left or right detect how ?

todaysprint avatar Oct 26 '22 12:10 todaysprint

You can get the current index directly on the swiper instance with swiperRef.current._reactInternals.memoizedState.index (swiperRef.current being the swiper instance)

pierrecabriere avatar Dec 12 '22 08:12 pierrecabriere