react-native-swiper: onIndexChanged produces Warning: Cannot update a component from inside the function body of a different component.
Which OS ?
iOS
Version
Which versions are you using:
- react-native-swiper v1.6.0
- react-native v0.63.2
Expected behaviour
No errors/warnings?
Actual behaviour
This code produces a warning, Warning: Cannot update a component from inside the function body of a different component.
.
import React, {useState} from 'react';
import Swiper from 'react-native-swiper';
const [swiperIndex, setSwiperIndex] = useState(0);
const testComponent = () => {
return <Swiper style={styles.wrapper} onIndexChanged={(i) => setSwiperIndex(i)}>
}
export default testComponent;
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 37
- Comments: 26
I use
onIndexChanged={(newIndex) => { setTimeout(() => { setIndex(newIndex); }, 1); }}
The warning is gone.Ugh, I just went away from this package and used
react-native-snap-carousel
instead. Sorry guys.@vietdiemtran I recommend to use react-native-snap-carousel instead. It’s easy to use, and it works.
This blog post about 16.13.0 says to wrap the setState call into useEffect. I’m not quite sure exactly what they mean or how to do that…
Same problem here, this is cased by react being updated to 16.13.0
Until there is a fix, you can do this nasty thing:
index.js
you can use useRef instead of state to store indexStep value
in my case, i fix by way onIndexChanged={(index) => setTimeout(() => { setIndex(index) }, 0) }
this fix is simple, I would do a PR myself but I no longer use this library as I ended up writing my own solution using a functional component which depends on a flatlist rather than using a scroll view. All that needs to be done to fix this is remove
and add
to the updateIndex function between line 519 and 520
How to I give like 100 likes? 🥇
Thanks for the recommendation…good library, well abstracted and seems relatively stable.
any update on this issue ?
Just use setTimeout:
I think React wants
react-native-swiper
to do something likeBut we have no control over it.
The library seems to be old, the code is written in an old fashioned way. Maybe it should be rewritten in a new React way.
Apparently its this here that causes it on line 213