Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trigger swipe with a button #111

Open
fatonramadani opened this issue Mar 22, 2016 · 18 comments
Open

Trigger swipe with a button #111

fatonramadani opened this issue Mar 22, 2016 · 18 comments

Comments

@fatonramadani
Copy link

How can I trigger a swipe ( Left/Right) with a button ?

@Kim-Andersen
Copy link

+1

1 similar comment
@PimDeWitte
Copy link

+1

@fatonramadani
Copy link
Author

I solve this by passing props to the Swiper component, and calling scrollTo in componentWillReceiveProps accordingly.

@PimDeWitte
Copy link

@drrazor can you share a code example?

@luiscript
Copy link

luiscript commented Jun 30, 2016

@PimDeWitte I had the same problem and I solve it exactly like @DrRazor said. Some sample code:

Setup your swiper like this:

<Swiper style={styles.wrapper} yourNewPageIndex={this.state.newIndex}>

then with the button that you want to trigger the swipe:

<Button onPress={() => this.setState({newIndex: 2})}/>

then in the swiper module change the componentWillReceiveProps like this:

componentWillReceiveProps(props) {
    this.setState(this.initState(props));    
    if(props.yourNewPageIndex){
      this.scrollBy(props.yourNewPageIndex)
    }
}

that's it... It works for me. I hope it works for you.

@PimDeWitte
Copy link

@luiscript thank you!

@RonakKhandelwal
Copy link

+1 We should have a prop like current page or something like that.

@gastonf-dev
Copy link

I did with ref. Sample code:
<Swiper ref='swiper' style={welcome.wrapper} showsButtons={false} loop={false} >
Then in the button.
onPress={() => this.refs.swiper.scrollBy(1)}

@The0racle
Copy link

I've achieved the same way @gferreyra91 did. I believe this is the best solution since updating the state with the new index and calling scrolling by with the new index seems to be doing the same thing.

I'm having issues however when the number of pages in the swiper is updated: scrollBy does not work unless I manually scroll the view.

@YannisDC
Copy link

Can you also pass this ref to a child component?
So they can have buttons that trigger the Swiper parent?

@rahls7
Copy link

rahls7 commented Jul 24, 2017

@YannisDC Did you figure out a way to pass it to the child component and scrolling accordingly?

@zawars
Copy link

zawars commented Nov 9, 2017

Doesn't works with loadMinimal = true @gferreyra91

@jkurei
Copy link

jkurei commented Dec 12, 2017

Because this still shows in Google Search results I'll add a bit on @gferreyra91 's solution. I don't think refs are long for this world, but it's still a fine solution for issues like this.

There is no need to pass the ref to the children as @rahls7 and @YannisDC suggest. I think it's much cleaner to handle the ref in its owner, and pass a function:

I define this method on the Swiper's parent:

swipe(n) {
    swiper = this.refs.swiper
    if (swiper) swiper.scrollBy(n || 1)
  }

And then, in the Items:

<Item 
    onPrevious={() => this.swipe(-1)}
    onNext={() => this.swipe(1)}
/>

As @gferreyra91 said, you have to assign the ref to the Swiper component:

<Swiper ref="swiper" {...otherProps}>{ pages }</Swiper>

PD: In spite of what @zawars comments, this is working fine for me with loadMinimal and loadMinimalSize={2}. May be they fixed that already.

@MewX
Copy link

MewX commented Jan 5, 2019

@YannisDC @gferreyra91

You could add a function prop for the child element.

e.g.

this.swiper = undefined;

<Swiper ref=(e => this.swiper = e) {...otherProps}>
  <View>
    <Child getSwiper={() => this.swiper} />
  </View>
</Swiper>

In this way, in the child component, you will be able to invoke:

this.props.getSwiper().scrollBy(1);

@rptoma
Copy link

rptoma commented Dec 8, 2019

The mentioned solution is pretty anti-pattern to be honest. The index property of Swiper should be observable. There are already warnings in RN 0.6.

@notmarinho
Copy link

Using React Hooks, this code bellow worked for me:

const swiper = useRef(null)

<Swiper
            ref={swiper}
            {...OtherProps}>
</Swiper>

<Button onPress={() => swiper.current.scrollTo(1) }/>

@ningacoding
Copy link

the mentioned anti-pattern it's true, you should pass actions instead of references/instances:

export default function SwiperComponent({}) {
  const swiperRef = useRef(null);
  const next = () => {
    if (!!swiperRef) {
      swiperRef.current.scrollBy(1);
    }
  };
  return <Swiper ref={swiperRef}>
      <ChildOne onNextPressed={() => next()} />
      <ChildTwo onNextPressed={() => next()} />
      <ChildThree onNextPressed={() => next()} />
    </Swiper>;
}

Children should do something like this (for example):

export default function ChildOne({onNextPressed}) {
  return <Button onPress={onNextPressed} />;
}

@elganiesta
Copy link

navigation={{
prevEl: '.classname-of-button-prev',
nextEl: '.classname-of-button-next',
}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests