@@ -89,6 +89,7 @@ export default class Carousel extends BaseComponent {
89
89
: props . pageWidth + props . itemSpacings || Constants . screenWidth ;
90
90
91
91
this . state = {
92
+ containerWidth : undefined ,
92
93
currentPage : this . shouldUsePageWidth ( ) ? this . getCalcIndex ( props . initialPage ) : props . initialPage ,
93
94
currentStandingPage : props . initialPage ,
94
95
pageWidth : defaultPageWidth ,
@@ -145,11 +146,31 @@ export default class Carousel extends BaseComponent {
145
146
return index ;
146
147
}
147
148
149
+ getSnapToOffsets = ( ) => {
150
+ const { itemSpacings} = this . props ;
151
+ const { containerWidth, pageWidth} = this . state ;
152
+
153
+ if ( containerWidth ) {
154
+ const spacings = pageWidth === containerWidth ? 0 : itemSpacings ;
155
+ const initialBreak = pageWidth - ( containerWidth - pageWidth - spacings ) / 2 ;
156
+ const snapToOffsets = _ . times ( presenter . getChildrenLength ( this . props ) , index => initialBreak + index * pageWidth ) ;
157
+ return snapToOffsets ;
158
+ }
159
+ }
160
+
148
161
shouldUsePageWidth ( ) {
149
162
const { loop, pageWidth} = this . props ;
150
163
return ! loop && pageWidth ;
151
164
}
152
165
166
+ onContainerLayout = ( { nativeEvent : { layout : { width : containerWidth } } } ) => {
167
+ const update = { containerWidth} ;
168
+ if ( ! this . props . pageWidth ) {
169
+ update . pageWidth = containerWidth ;
170
+ }
171
+ this . setState ( update ) ;
172
+ }
173
+
153
174
onContentSizeChange = ( ) => {
154
175
// this is to handle initial scroll position (content offset)
155
176
if ( Constants . isAndroid ) {
@@ -200,6 +221,11 @@ export default class Carousel extends BaseComponent {
200
221
} ;
201
222
202
223
renderChildren ( ) {
224
+ const { containerWidth} = this . state ;
225
+ if ( ! containerWidth && ! this . shouldUsePageWidth ( ) ) {
226
+ return null ;
227
+ }
228
+
203
229
const { children, loop} = this . props ;
204
230
const length = presenter . getChildrenLength ( this . props ) ;
205
231
@@ -257,15 +283,13 @@ export default class Carousel extends BaseComponent {
257
283
258
284
render ( ) {
259
285
const { containerStyle, itemSpacings, ...others } = this . props ;
260
- const { initialOffset, pageWidth } = this . state ;
286
+ const { initialOffset} = this . state ;
261
287
262
288
const scrollContainerStyle = this . shouldUsePageWidth ( ) ? { paddingRight : itemSpacings } : undefined ;
263
- const spacings = pageWidth === Constants . screenWidth ? 0 : itemSpacings ;
264
- const initialBreak = pageWidth - ( Constants . screenWidth - pageWidth - spacings ) / 2 ;
265
- const snapToOffsets = _ . times ( presenter . getChildrenLength ( this . props ) , index => initialBreak + index * pageWidth ) ;
289
+ const snapToOffsets = this . getSnapToOffsets ( ) ;
266
290
267
291
return (
268
- < View style = { containerStyle } >
292
+ < View style = { containerStyle } onLayout = { this . onContainerLayout } >
269
293
< ScrollView
270
294
{ ...others }
271
295
ref = { this . carousel }
0 commit comments