[Discussion] setPage should not trigger onPageSelected event
See original GitHub issueBug
We are storing and updating the active index of the ViewPager in Redux. When the user swipes to another page the onPageSelected
event is correctly triggered and the active index will be updated like that:
<ViewPager
ref={pager}
pageMargin={0}
onPageSelected={({ nativeEvent }) => setActiveIndex(nativeEvent.position)}
style={[styles.pager, { width }]}
initialPage={0}
>
But we also want to set the page programatically when the active index is changed by some other component. To achieve this we are using an useEffect hook like this:
useEffect(() => {
pager.current && pager.current.setPageWithoutAnimation(activeIndex);
}, [activeIndex]);
Unfortunately calling setPageWithoutAnimation
triggers another onPageSelected
event which updates the active index which triggers the useEffect
hook and so on.
We think that there should be no onPageSelected
event triggered when setting the page programatically.
Environment info
React native info output:
System:
OS: macOS Mojave 10.14.6
CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
Memory: 5.96 GB / 16.00 GB
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 10.16.3 - ~/.nvm/versions/node/v10.16.3/bin/node
Yarn: 1.19.1 - /usr/local/bin/yarn
npm: 6.13.1 - ~/.nvm/versions/node/v10.16.3/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1
Android SDK:
API Levels: 21, 22, 23, 24, 25, 26, 27, 28
Build Tools: 23.0.1, 25.0.2, 25.0.3, 26.0.1, 26.0.2, 26.0.3, 27.0.3, 28.0.1, 28.0.3, 29.0.2
System Images: android-19 | Google APIs Intel x86 Atom, android-21 | Google APIs Intel x86 Atom_64, android-22 | Intel x86 Atom_64, android-22 | Google APIs Intel x86 Atom_64, android-23 | Google APIs Intel x86 Atom_64, android-26 | Intel x86 Atom_64, android-27 | Google APIs Intel x86 Atom, android-28 | Google APIs Intel x86 Atom
Android NDK: 20.1.5948944
IDEs:
Android Studio: 3.5 AI-191.8026.42.35.6010548
Xcode: 11.3/11C29 - /usr/bin/xcodebuild
npmPackages:
react: 16.9.0 => 16.9.0
react-native: 0.61.5 => 0.61.5
Library version: 3.3.0
Issue Analytics
- State:
- Created 4 years ago
- Reactions:11
- Comments:25 (5 by maintainers)
Top Results From Across the Web
onPageSelected isn't triggered when calling setCurrentItem(0)
Everything works, except when setCurrentItem called with index set to 0 as this will not trigger onPageSelected. mPager.setOnPageChangeListener( ...
Read more >Setting Selectedindex Not Triggering Onchange Event
[Discussion] setPage should not trigger onPageSelected event #129 should be no onPageSelected event triggered when setting the page.
Read more >ViewPager OnPageChangeListener is not called for initial ...
We apply some transformations to the views once a new page is selected and the fact this event is not called for the...
Read more >Android Notes for Professionals - Free PDF Books
not a liated with o cial Android™ group(s) or company(s). ... Section 43.1: Using the same click event for one or...
Read more >Android Notes for Professionals - GoalKicker.com
not a liated with o cial Android™ group(s) or company(s). ... Section 43.1: Using the same click event for one or...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ideally would be good to have react-native-viewpager as controlled component. Instead of
initialPage
might be better to havepage
which will control selected page.@crystalneth not yet. what you describe is exactly our problem. I’ll take a closer look this week and let you know.