1
1
import React , { useState } from 'react'
2
- import { ListRenderItem , View } from 'react-native'
2
+ import { InteractionManager , ListRenderItem , Platform , View } from 'react-native'
3
3
import Carousel , { Pagination } from 'react-native-snap-carousel'
4
4
5
+ import { useAsyncEffect } from '../../hooks/useAsyncEffect'
5
6
import { useHandler } from '../../hooks/useHandler'
6
7
import { cacheStyles , Theme , useTheme } from '../services/ThemeContext'
7
8
@@ -26,16 +27,47 @@ export function EdgeCarousel<T>(props: Props<T>): JSX.Element {
26
27
const carouselRef = React . useRef < Carousel < any > > ( null )
27
28
28
29
const [ activeIndex , setActiveIndex ] = useState ( 0 )
30
+ const [ dataLocal , setDataLocal ] = useState ( data )
31
+
32
+ React . useEffect ( ( ) => {
33
+ setDataLocal ( data )
34
+ } , [ data ] )
29
35
30
36
const renderItem = useHandler < ListRenderItem < T > > ( info => (
31
37
< View style = { [ styles . childContainer , { width : width * 0.9 , height } ] } > { props . renderItem ( info ) } </ View >
32
38
) )
33
39
40
+ /**
41
+ * Carousel's FlatList bug workaround. Fixes the issue where items are
42
+ * hidden until scroll actions are performed either in the carousel or on the
43
+ * scene itself.
44
+ */
45
+ useAsyncEffect (
46
+ async ( ) => {
47
+ // HACK: With 1 item, this is the only way to force a render in iOS
48
+ if ( Platform . OS === 'ios' && dataLocal . length === 1 ) {
49
+ const tempData = [ ...dataLocal ]
50
+ setDataLocal ( [ ] )
51
+ setTimeout ( ( ) => {
52
+ setDataLocal ( tempData )
53
+ } , 500 )
54
+ }
55
+ // The built-in hack fn works for all other cases
56
+ else if ( carouselRef . current != null ) {
57
+ await InteractionManager . runAfterInteractions ( ( ) => {
58
+ carouselRef . current ?. triggerRenderingHack ( )
59
+ } )
60
+ }
61
+ } ,
62
+ [ dataLocal ] , // Depend on dataLocal instead of data to avoid infinite loops
63
+ 'triggerRenderingHack'
64
+ )
65
+
34
66
return (
35
67
< View style = { styles . carouselContainer } >
36
68
< Carousel
37
69
ref = { carouselRef }
38
- data = { data }
70
+ data = { dataLocal }
39
71
keyExtractor = { keyExtractor }
40
72
renderItem = { renderItem }
41
73
sliderWidth = { width }
@@ -55,7 +87,7 @@ export function EdgeCarousel<T>(props: Props<T>): JSX.Element {
55
87
marginTop : - theme . rem ( 1 ) ,
56
88
marginBottom : - theme . rem ( 1 )
57
89
} }
58
- dotsLength = { data . length }
90
+ dotsLength = { dataLocal . length }
59
91
activeDotIndex = { activeIndex }
60
92
tappableDots = { carouselRef . current != null }
61
93
dotStyle = { styles . dotStyle }
0 commit comments