swiper/react onSlideChange's callback wouldn't be updated
See original GitHub issueThis is a (multiple allowed):
-
bug
-
enhancement
-
feature-discussion (RFC)
-
Swiper Version: 6.1.1
-
Platform/Target and Browser Versions: macOS
-
Live Link or JSFiddle/Codepen or website with isssue: https://codesandbox.io/s/swiper-event-listener-5h5jo?file=/src/App.js
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 becauselist
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:
- Created 3 years ago
- Reactions:13
- Comments:6
useRef
as another workaround to get latest state value in the component whenonSlideChange
not get updated.based on your example
https://codesandbox.io/s/swiper-event-listener-forked-pe3ex?file=/src/App.js:0-1295
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.