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

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

Open ngoctruy87 opened this issue 10 years ago • 6 comments

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

ngoctruy87 avatar Oct 25 '16 16:10 ngoctruy87

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 avatar Oct 28 '16 14:10 leaky

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

shkfnly avatar Dec 06 '16 16:12 shkfnly

@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.

leaky avatar Dec 06 '16 19:12 leaky

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.

xvaldetaro avatar Dec 13 '16 03:12 xvaldetaro

"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>

michaelknoch avatar Dec 20 '16 14:12 michaelknoch

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>

VienLeVan avatar Aug 26 '19 09:08 VienLeVan