React-native-swiper: jump to a slide by number. what must i do?

Created on 25 Oct 2016  路  6Comments  路  Source: leecade/react-native-swiper

jump to a slide by number. what must i do?
thank

Most helpful comment

I ran into this problem and discovered that the best way to do this is to add a ref to your Swiper component and then pass a function as props to your child components inside Swiper and then use the value from the props function to navigate the slider like so:

jumpToSlide (value) {
    this.refs.swiper.scrollBy(value) // n is the number of places to move the swipe, eg: 2, -1, etc.
}

render () {
    return (
        <Swiper ref="swiper">
            <Child swipe={(value) => this.jumpToSlide(value)} />
        </Swiper>
    )
}

Then inside Child:
<Text onPress={() => this.props.swipe(1)}>Swipe!</Text>

All 6 comments

I ran into this problem and discovered that the best way to do this is to add a ref to your Swiper component and then pass a function as props to your child components inside Swiper and then use the value from the props function to navigate the slider like so:

jumpToSlide (value) {
    this.refs.swiper.scrollBy(value) // n is the number of places to move the swipe, eg: 2, -1, etc.
}

render () {
    return (
        <Swiper ref="swiper">
            <Child swipe={(value) => this.jumpToSlide(value)} />
        </Swiper>
    )
}

Then inside Child:
<Text onPress={() => this.props.swipe(1)}>Swipe!</Text>

@leaky Did you find an easy way to update the animation speed, it seems that mine moves really slowly using your method.

@shkfnly if it's moving really slowly check that:

  1. You don't have slow animations enabled by pressing Cmd+T.
  2. If using Chrome, you don't have other tabs open in the same window that the React Native Debugger is open - I don't know why but this does cause the simulator to run slowly/laggy.

When doing this scrollBy hack, the pagination doesn't update, you have to also call updateIndex.
I want to just reset the slide to the first one which makes it even hackier because I have to find what is the current slide.

"Currently, two ways are supported by React to refer to components. The first one, providing a string identifier is considered legacy in the official documentation. Referring to components by setting a property on the this object in the reference callback is preferred."
https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-string-refs.md

jumpToSlide(value) {
    this.swiper.scrollBy(value);
}

render () {
    return (
        <Swiper ref={(component) => { this.swiper = component; }}>
            <Child swipe={slide => this.jumpToSlide(slide)} />
        </Swiper>
    )
}
<Text onPress={() => this.props.swipe(1)}>Swipe!</Text>

I ran into this problem and discovered that the best way to do this is to add a ref to your Swiper component and then pass a function as props to your child components inside Swiper and then use the value from the props function to navigate the slider like so:

jumpToSlide (value) {
    this.refs.swiper.scrollBy(value) // n is the number of places to move the swipe, eg: 2, -1, etc.
}

render () {
    return (
        <Swiper ref="swiper">
            <Child swipe={(value) => this.jumpToSlide(value)} />
        </Swiper>
    )
}

Then inside Child:
<Text onPress={() => this.props.swipe(1)}>Swipe!</Text>

Was this page helpful?
0 / 5 - 0 ratings

Related issues

agzuniverse picture agzuniverse  路  3Comments

tokict picture tokict  路  3Comments

commit-master picture commit-master  路  3Comments

ruben-kasaz picture ruben-kasaz  路  3Comments

Liqiankun picture Liqiankun  路  3Comments