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 onSlideChange's callback wouldn't be updated

See original GitHub issue

This is a (multiple allowed):

What you did

A callback in my react component is set to swiper/react component, and I want to update that function

Expected Behavior

callback function would be updated

Actual Behavior

please refer to the provided codesandbox link, reproducing steps:

  • click the random button, the list is updated
  • change active slide with navigation, the List in onSlideChange Cb wouldn’t be updated because list in the callback stays the same

I am not sure if this is related to how event listeners are updated to the swiper instance from react component. I quickly checked the source code but I can’t find any clue. I think it might be much easier to look for help here so if someone can give me some hint that would be really appreciated!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:13
  • Comments:6

github_iconTop GitHub Comments

3reactions
JaosnHsiehcommented, Nov 17, 2020

useRef as another workaround to get latest state value in the component when onSlideChange not get updated.


import React, { useState, useCallback, useRef } from "react";
import SwiperCore, { Navigation } from "swiper";
import { Swiper, SwiperSlide } from "swiper/react";
import "./styles.css";

SwiperCore.use([Navigation]);

export default function App() {
  const listRef = useRef(null);
  const [list, setList] = useState([
    {
      id: "1"
    },
    {
      id: "2"
    }
  ]);
  const setListAndRef = (l) => {
    setList(l);
    listRef.current = l;
  };
  const [listInCb, setListInCb] = useState(list);

  const onSlideChange = useCallback(() => {
    // list stays the same as the intial value
    console.log(JSON.stringify(listRef.current));
    setListInCb(listRef.current);
  }, []);

  const onRandomClick = useCallback(() => {
    setListAndRef([
      {
        id: Math.floor(1000 * Math.random())
      },
      {
        id: Math.floor(1000 * Math.random())
      }
    ]);
  }, []);

  const items = list.map((li) => (
    <SwiperSlide key={li.id}>{li.id}</SwiperSlide>
  ));
  return (
    <div className="App">
      <Swiper navigation onSlideChange={onSlideChange}>
        {items}
      </Swiper>
      <button type="button" onClick={onRandomClick}>
        Random list
      </button>
      <p>List in onSlideChange Cb</p>
      {JSON.stringify(listInCb)}
    </div>
  );
}

based on your example

https://codesandbox.io/s/swiper-event-listener-forked-pe3ex?file=/src/App.js:0-1295

1reaction
saveman71commented, Feb 26, 2021

From what I can gather, https://github.com/nolimits4web/swiper/blob/f8ca5c094b7fa5d361b80510340256ecbfca75f9/src/react/get-changed-params.js should properly notice that the event handler changed.

Then https://github.com/nolimits4web/swiper/blob/f8ca5c094b7fa5d361b80510340256ecbfca75f9/src/react/update-swiper.js doesn’t do anything with that info.

It would need to .off and .on the changed event handlers.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Swiper.js slide won't change when updating state with ...
I'm trying to change the {quesNumber} state with swiper's onSlideChange method, which should set the "q" URL parameter based on the current ......
Read more >
Swiper React Components
Swiper is the most modern free mobile touch slider with hardware accelerated transitions and amazing native behavior.
Read more >
swiper.activeindex react | The AI Search Engine You Control
I have tried to handle the onSwiper function with props but still cant update the slide index. Thank you. reactjs; swiper.js; Share. Improve...
Read more >
Animation Add-Ons - React
Please file bugs and feature requests in the new repository. The ReactTransitionGroup add-on component is a low-level API for animation, ...
Read more >
Safari Technology Preview Release Notes - Apple Developer
Fixed dynamic aria-disabled changes not updating enabled status for descendants ... Allowed history swipe in scroller with overscroll-behavior (r291497) ...
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