question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Swiper React custom navigation components

See original GitHub issue

This is a:

  • bug

  • enhancement

  • feature-discussion (RFC)

  • Swiper Version: swiper@6.0.1.

  • Live Link or JSFiddle/Codepen or website with isssue: PREFERABLY (IF YOU WANT YOUR ISSUE TO BE RESOLVED ASAP).

What you did

I set navigation property <Swiper navigation={{ navigation: { nextEl: (<div className='next'></div>), prevEl: (<div className='next'></div>) }}> ... </Swiper>

Expected Behavior

I want to see my own navigation buttons

Actual Behavior

The navigation isn’t there, nither the original. It’s like “navigation: false”.

Thanks in advance

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

10reactions
nolimits4webcommented, Sep 3, 2020

As to documentation nextEl and prevEl receive HTMLElement or string with CSS selector, so it should be:

  <Swiper
    navigation={{
      prevEl: '.prev',
      nextEl: '.next',
    }}
  >
    <SwiperSlide>slide 1</SwiperSlide>
    <SwiperSlide>slide 2</SwiperSlide>
    <div className="prev" />
    <div className="next" />
  </Swiper>
9reactions
lucaskahlcommented, Sep 1, 2020

@awt0523 If you check the Typescript interface from navigation, it can receives an object with nextEl and prevEl and both can receive a CSSSelector or HTMLElement and yeah, for some reason they aren’t showing.

@MacGyver98 To get around this, I ended up creating a useRef in the swipe to access the slideNext and slidePrev methods, here’s an example.

import React, { useCallback, useRef } from 'react';
import { Swiper, SwiperSlide } from 'swiper/react';
import SwiperCore, { Autoplay } from 'swiper';

function Swiper() {
  const swiperRef = useRef(null);
  SwiperCore.use([Autoplay]); // don't need navigation anymore

  const prevSlide = useCallback(() => {
    swiperRef.current?.swiper.slidePrev();
  }, [swiperRef]);

  const nextSlide = useCallback(() => {
    swiperRef.current?.swiper.slideNext();
  }, [swiperRef]);

  return (
    <>
      <div className="left-floating-el" onClick={prevSlide}>
        an floating element
      </div>
      <Swiper
        ref={swiperRef}
        spaceBetween={30}
        slidesPerView={5}
        autoplay={{ delay: 5000 }}
        loop
      >
        <SwiperSlide>slide 1</SwiperSlide>
        <SwiperSlide>slide 2</SwiperSlide>
      </Swiper>
      <div className="right-floating-el" onClick={nextSlide}>
        an floating element
      </div>
    </>
  );
}

Read more comments on GitHub >

github_iconTop Results From Across the Web

Swiper React | How to create custom navigation/pagination ...
Thank you for this. I was wondering if there is a way to get the swiper instance at the root component like this....
Read more >
[swiper/react] Custom navigation/pagination components ...
SwiperJS documentation states that navigation prevEl/nextEl can either be of type "string" or "HTMLElement". Using HTML nodes allows for ...
Read more >
react-swiper-navigation - CodeSandbox
CodeSandbox is an online editor tailored for web applications.
Read more >
Swiper React Components
Note, Swiper React component will create required elements for Navigation, Pagination and Scrollbar if you pass these params without specifying its elements ......
Read more >
How to Customize Prev/Next Buttons in React SwiperJs
How to Customize Prev/Next Buttons in React SwiperJs · 1. Change Color of Swiper Arrows · 2. Replace Image of Swiper Arrows.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found