swiper icon indicating copy to clipboard operation
swiper copied to clipboard

Swiper Navigation Events Are Triggered Twice When Using Breakpoints

Open corygottschalk opened this issue 2 years ago • 11 comments

Check that this is really a bug

  • [X] I confirm

Reproduction link

https://codesandbox.io/s/swiper-duplicate-navigation-event-bindings-mby46x?file=/index.html

Bug description

While conditionally enabling navigation in a breakpoint while disabling it on mobile results in slide navigation events being bound twice in the navigation module.

I narrowed the issue down to enable being called in /core/breakpoints/setBreakpoint.js and init being called in the init event handler in /modules/navigation/navigation.js.

I am unsure when this bug was introduced.

Expected Behavior

The slider should only advance one slide when clicking on a navigation button.

Only one event binding should be created for each navigation button during initialization.

Actual Behavior

The slider advances twice, skipping a slide.

Swiper version

8.4.5

Platform/Target and Browser Versions

macOS 12.6.1, arm 64, Google Chrome 108.0.5359.98,

Validations

  • [X] Follow our Code of Conduct
  • [X] Read the docs.
  • [X] Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
  • [X] Make sure this is a Swiper issue and not a framework-specific issue

Would you like to open a PR for this bug?

  • [X] I'm willing to open a PR

corygottschalk avatar Dec 09 '22 19:12 corygottschalk

For anyone else experiencing this issue, I was able to add the below code to the config to clean up the event bindings in the meantime.

{
    on: {
        init(swiper: Swiper): void {
            swiper.navigation?.destroy();
            const breakpoint: string = swiper.getBreakpoint(swiper.params.breakpoints);
            const currentBreakpoint: SwiperOptions | undefined = swiper.params.breakpoints?.[breakpoint];
            const { navigation: currentBreakpointNavConfig } = currentBreakpoint || {};

            if (!currentBreakpointNavConfig) {
                return;
            }

            if (currentBreakpointNavConfig === true || currentBreakpointNavConfig?.enabled) {
                swiper.navigation.init();
                swiper.navigation.update();
            }
        },
    },
}

corygottschalk avatar Dec 09 '22 20:12 corygottschalk

I have the same problem.

Ricardo-Bonin avatar Dec 26 '22 19:12 Ricardo-Bonin

Same here. Thank you for the fix @corygottschalk

ghost avatar Feb 09 '23 10:02 ghost

Had this problem with @ionic/react. Found that swiper.navigation module was getting initialized twice, creating two event listeners for the prev/next buttons. My workaround was to override the onInit method.

 onInit={(swiper) => {
        if (!!swiper.navigation) {
          swiper.navigation.destroy();
        }
      }}

davetisyan95 avatar Jun 14 '23 10:06 davetisyan95

Can confirm, still happening in v9

edtorba avatar Sep 13 '23 15:09 edtorba

Having the issue now in React. Not solved yet. still happening.

amrfateem avatar Oct 04 '23 19:10 amrfateem

if (!!swiper.navigation) { swiper.navigation.destroy(); }

Your solution works great. I added it and used Patch-Manager to use it on my build. consider adding it as a fork so no one has this issue again

amrfateem avatar Oct 05 '23 07:10 amrfateem

I also see this (or very similar?) behavior with Swiper 11.0.6 while using Swiper Element (WebComponent) on Angular. Under the debugger it can be seen that initialize() is called by me only once, but navigation.mjs::init() fires twice and then each navigation click causes two onNextClick events...

It's pretty crazy that such issue remains open for so long.

POlczak-ITTouch avatar Feb 07 '24 12:02 POlczak-ITTouch

Swiper Element native JS 11.1.1 version - the same behaviour - swipernavigationnext and swipernavigationprev - fires two times

PimiTree avatar Apr 22 '24 07:04 PimiTree

I just had this issue. I've looked into it and the Navigation-Module is indeed initialized twice.

In our case this was the case, because we added the Navigation-Module to the modules within the swiper-config.

// this causes the error
new Swiper(sliderElement, {
    modules: [Pagination, Navigation],
    // ...
});

// this works
new Swiper(sliderElement, {
    modules: [],
    // ...
});

Navigation and Pagination are added by default to Swiper. If they are listed in modules, they are added again which causes this issue.

mkrauser avatar Jun 25 '24 07:06 mkrauser