# Changing slides using buttons above swiper

**URL:** <https://forum.ionicframework.com/t/changing-slides-using-buttons-above-swiper/220380>\
**Category:** Ionic React\
**Created:** [February 11, 2022, 8:47am UTC](https://forum.ionicframework.com/t/changing-slides-using-buttons-above-swiper/220380 "2022-02-11T08:47:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![eugenelet](https://avatars.discourse-cdn.com/v4/letter/e/c77e96/32.png) [@eugenelet](https://forum.ionicframework.com/u/eugenelet)\
**Post date:** [February 11, 2022, 8:47am UTC](https://forum.ionicframework.com/t/changing-slides-using-buttons-above-swiper/220380/1 "2022-02-11T08:47:34Z")

</div>

Hi there, I’m new to Ionic React.

I’m trying to change slides using buttons that are placed above Swiper onClick:

```auto
const Tab: React.FC = () => {
  const swiper = useSwiper();

  return (
    <IonPage>
        <IonItem class="labels">
          <IonButton onClick={() => swiper.slideNext() }>
            <IonIcon icon={chatbubbles} color="primary" />
            <IonLabel> Chat</IonLabel>
          </IonButton>
        </IonItem>
        <Swiper className="mySwiper"
          onSwiper={(swiper) => console.log(swiper)}
          onSlideChange={() => console.log('slide change')}
        >
          <SwiperSlide>Chat</SwiperSlide>
          <SwiperSlide>Stories</SwiperSlide>
          <SwiperSlide>Topics</SwiperSlide>
          <SwiperSlide>Private</SwiperSlide>
        </Swiper>
    </IonPage>
  );
};

```

Since `Swiper` is not initialized before the button, how do I pass `swiper` to `onClick`? E.g.

```auto
Uncaught TypeError: Cannot read properties of null (reading 'slideNext')

```

Thanks!

---

<div class="post-metadata">

**Author:** ![aaronksaunders](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/aaronksaunders/32/13729_2.png) [@aaronksaunders](https://forum.ionicframework.com/u/aaronksaunders)\
**Post date:** [February 12, 2022, 3:47pm UTC](https://forum.ionicframework.com/t/changing-slides-using-buttons-above-swiper/220380/2 "2022-02-12T15:47:09Z")

</div>

See full source and video here.

[![](https://global.discourse-cdn.com/ionicframework/original/3X/c/c/ccb836698ba63c5911eec102c62f9138b02cb2fa.jpeg "Creating Slides with ReactJS and Ionic Framework - Accessing The Full SwiperJS API") ](https://www.youtube.com/watch?v=mCkrZYIbH10)

Reference from answer on stackoverflow - [reactjs - Ionic v5 React Ion-Slides: How to make a button to the next slide? - Stack Overflow](https://stackoverflow.com/a/66735500)

---

<div class="post-metadata">

**Author:** ![javier-soto-dev15](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/javier-soto-dev15/32/140804_2.png) [@javier-soto-dev15](https://forum.ionicframework.com/u/javier-soto-dev15)\
**Post date:** [February 12, 2022, 6:59pm UTC](https://forum.ionicframework.com/t/changing-slides-using-buttons-above-swiper/220380/3 "2022-02-12T18:59:47Z")

</div>

It’s easy  
add this to your swiper navigation option  
`navigation={{ nextEl: "#swiper-forward", prevEl: "#swiper-back" }}`  
and then add the id to your buttons anywhere in the page

```auto
           <IonButton id="swiper-back" fill="clear">
              <IonIcon slot="icon-only" icon={chevronBackOutline}/>
            </IonButton>

```

Example of the structure

```auto
          <Swiper
            modules={[Navigation, IonicSlides]}
            navigation={{ nextEl: "#swiper-forward", prevEl: "#swiper-back" }}
          >
...
           <IonButton id="swiper-back" fill="clear">
              <IonIcon slot="icon-only" icon={chevronBackOutline}/>
            </IonButton>
            <IonButton id="swiper-forward" fill="clear">
              <IonIcon slot="icon-only" icon={chevronForwardOutline}/>
            </IonButton>

```

---

<div class="post-metadata">

**Author:** ![eugenelet](https://avatars.discourse-cdn.com/v4/letter/e/c77e96/32.png) [@eugenelet](https://forum.ionicframework.com/u/eugenelet)\
**Post date:** [February 13, 2022, 4:36pm UTC](https://forum.ionicframework.com/t/changing-slides-using-buttons-above-swiper/220380/4 "2022-02-13T16:36:24Z")

</div>

Thanks for the video. I’ve tried your solution before posting this. I got the same error as one of the comments in the video:

```auto
TypeScript error in /Users/macintosh/Desktop/theMagger/src/components/SlideContainer.tsx(19,26):
Object is possibly 'null'. TS2531

    17 | 
    18 | const handleSlideChange = async () => {
  > 19 | const swiper = await mySlides.current.getSwiper();
       | ^
    20 | // setDisablePrevBtn(swiper.isBeginning);
    21 | // setDisableNextBtn(swiper.isEnd);
    22 | };

```

---

<div class="post-metadata">

**Author:** ![eugenelet](https://avatars.discourse-cdn.com/v4/letter/e/c77e96/32.png) [@eugenelet](https://forum.ionicframework.com/u/eugenelet)\
**Post date:** [February 13, 2022, 4:39pm UTC](https://forum.ionicframework.com/t/changing-slides-using-buttons-above-swiper/220380/5 "2022-02-13T16:39:36Z")

</div>

Thanks for posing your solution. I’m thinking about navigating to any slide in the list of slides created. Your solution is designed to move back and forth between slides. Can it also be done using the `navigation` option for `Slider`?

---

<div class="post-metadata">

**Author:** ![aaronksaunders](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/aaronksaunders/32/13729_2.png) [@aaronksaunders](https://forum.ionicframework.com/u/aaronksaunders)\
**Post date:** [February 13, 2022, 4:47pm UTC](https://forum.ionicframework.com/t/changing-slides-using-buttons-above-swiper/220380/6 "2022-02-13T16:47:55Z")

</div>

You need to just check for null and the error will go away.

---

<div class="post-metadata">

**Author:** ![javier-soto-dev15](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/javier-soto-dev15/32/140804_2.png) [@javier-soto-dev15](https://forum.ionicframework.com/u/javier-soto-dev15)\
**Post date:** [February 14, 2022, 9:37pm UTC](https://forum.ionicframework.com/t/changing-slides-using-buttons-above-swiper/220380/7 "2022-02-14T21:37:31Z")

</div>

You could use navigation but i dont mess up with that module a lot. There is a method in the swiper controller called “slideTo”, essentially takes the swiper view to the slide you pick. I am linking the doc for it [Swiper API](https://swiperjs.com/swiper-api#method-swiper-slideTo)

---

<div class="post-metadata">

**Author:** ![eugenelet](https://avatars.discourse-cdn.com/v4/letter/e/c77e96/32.png) [@eugenelet](https://forum.ionicframework.com/u/eugenelet)\
**Post date:** [February 15, 2022, 8:23am UTC](https://forum.ionicframework.com/t/changing-slides-using-buttons-above-swiper/220380/8 "2022-02-15T08:23:24Z")

</div>

Thanks! This is what I needed.

---

<div class="post-metadata">

**Author:** ![eugenelet](https://avatars.discourse-cdn.com/v4/letter/e/c77e96/32.png) [@eugenelet](https://forum.ionicframework.com/u/eugenelet)\
**Post date:** [February 15, 2022, 8:23am UTC](https://forum.ionicframework.com/t/changing-slides-using-buttons-above-swiper/220380/9 "2022-02-15T08:23:59Z")

</div>

It seems to break on the current version. A workaround would be something like:

```auto
  const [swiperRef, setSwiperRef] = useState<any | null>(null);

```
