onChangeIndex called repeatedly when setState in it.
diamont1001 opened this issue · comments
When calling setState in the onChangeIndex function, it will cause the onChangeIndex function to be called repeatedly. No matter what state is modified by setState.
Platform: Android
React Native version:
System:
OS: macOS 13.2.1
CPU: (8) x64 Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
Memory: 1.62 GB / 16.00 GB
Shell:
version: 5.8.1
path: /bin/zsh
Binaries:
Node:
version: 18.16.0
path: ~/.nvm/versions/node/v18.16.0/bin/node
Yarn:
version: 1.22.19
path: ~/.nvm/versions/node/v18.16.0/bin/yarn
npm:
version: 9.6.5
path: ~/.nvm/versions/node/v18.16.0/bin/npm
Watchman:
version: 2023.06.26.00
path: /usr/local/bin/watchman
Managers:
CocoaPods:
version: 1.12.1
path: /usr/local/bin/pod
SDKs:
iOS SDK:
Platforms:
- DriverKit 22.4
- iOS 16.4
- macOS 13.3
- tvOS 16.4
- watchOS 9.4
Android SDK:
API Levels:
- "25"
- "27"
- "28"
- "29"
- "31"
- "33"
- "34"
Build Tools:
- 28.0.3
- 29.0.3
- 30.0.3
- 31.0.0
- 33.0.0
System Images:
- android-29 | Google APIs Intel x86 Atom
- android-31 | Google APIs Intel x86_64 Atom
- android-33 | Google APIs Intel x86_64 Atom
- android-34 | Google APIs Intel x86_64 Atom
Android NDK: Not Found
IDEs:
Android Studio: 2022.1 AI-221.6008.13.2211.9619390
Xcode:
version: 14.3.1/14E300c
path: /usr/bin/xcodebuild
Languages:
Java:
version: 11.0.18
path: /usr/bin/javac
Ruby:
version: 2.6.10
path: /usr/bin/ruby
npmPackages:
"@react-native-community/cli": Not Found
react:
installed: 18.2.0
wanted: 18.2.0
react-native:
installed: 0.72.3
wanted: 0.72.3
react-native-macos: Not Found
npmGlobalPackages:
"*react-native*": Not Found
Android:
hermesEnabled: true
newArchEnabled: false
iOS:
hermesEnabled: true
newArchEnabled: false
Steps To Reproduce
- SwiperFlatList
- this.setState in onChangeIndex
Describe what you expected to happen:
Snack, code example, screenshot, or link to a repository:
<SwiperFlatList
...
onChangeIndex={({index}) => {
this.setState({abc: 1})
}}
/>
I encountered the same problem
@diamont1001
You can try using useCallback to wrap the onChangeIndex event
@diamont1001 You can try using useCallback to wrap the onChangeIndex event
Thx, I'm using PaginationComponent
and paginationIndex
, so there's no need to access index
from onChangeIndex
now :)