@@ -102,6 +102,7 @@ export default class extends Component {
102102 static propTypes = {
103103 horizontal : PropTypes . bool ,
104104 children : PropTypes . node . isRequired ,
105+ containerStyle : ViewPropTypes . style ,
105106 style : ViewPropTypes . style ,
106107 pagingEnabled : PropTypes . bool ,
107108 showsHorizontalScrollIndicator : PropTypes . bool ,
@@ -156,7 +157,7 @@ export default class extends Component {
156157 * Init states
157158 * @return {object } states
158159 */
159- state = this . initState ( this . props , true )
160+ state = this . initState ( this . props )
160161
161162 /**
162163 * autoplay timer
@@ -166,10 +167,8 @@ export default class extends Component {
166167 loopJumpTimer = null
167168
168169 componentWillReceiveProps ( nextProps ) {
169- const sizeChanged = ( nextProps . width || width ) !== this . state . width ||
170- ( nextProps . height || height ) !== this . state . height
171170 if ( ! nextProps . autoplay && this . autoplayTimer ) clearTimeout ( this . autoplayTimer )
172- this . setState ( this . initState ( nextProps , sizeChanged ) )
171+ this . setState ( this . initState ( nextProps ) )
173172 }
174173
175174 componentDidMount ( ) {
@@ -181,7 +180,7 @@ export default class extends Component {
181180 this . loopJumpTimer && clearTimeout ( this . loopJumpTimer )
182181 }
183182
184- initState ( props , setOffsetInState ) {
183+ initState ( props ) {
185184 // set the current state
186185 const state = this . state || { }
187186
@@ -200,8 +199,6 @@ export default class extends Component {
200199 // retain the index
201200 initState . index = state . index
202201 } else {
203- // reset the index
204- setOffsetInState = true // if the index is reset, go ahead and update the offset in state
205202 initState . index = initState . total > 1 ? Math . min ( props . index , initState . total - 1 ) : 0
206203 }
207204
@@ -211,22 +208,6 @@ export default class extends Component {
211208 initState . height = props . height || height
212209 newInternals . offset = { }
213210
214- if ( initState . total > 1 ) {
215- let setup = initState . index
216- if ( props . loop ) {
217- setup ++
218- }
219- newInternals . offset [ initState . dir ] = initState . dir === 'y'
220- ? initState . height * setup
221- : initState . width * setup
222- }
223-
224- // only update the offset in state if needed, updating offset while swiping
225- // causes some bad jumping / stuttering
226- if ( setOffsetInState ) {
227- initState . offset = newInternals . offset
228- }
229-
230211 this . internals = newInternals
231212 return initState
232213 }
@@ -236,6 +217,29 @@ export default class extends Component {
236217 return Object . assign ( { } , this . state , this . internals )
237218 }
238219
220+ onLayout = ( event ) => {
221+ const { width, height } = event . nativeEvent . layout
222+ const offset = this . internals . offset = { }
223+ const state = { width, height }
224+
225+ if ( this . state . total > 1 ) {
226+ let setup = this . state . index
227+ if ( this . props . loop ) {
228+ setup ++
229+ }
230+ offset [ this . state . dir ] = this . state . dir === 'y'
231+ ? height * setup
232+ : width * setup
233+ }
234+
235+ // only update the offset in state if needed, updating offset while swiping
236+ // causes some bad jumping / stuttering
237+ if ( width !== this . state . width || height !== this . state . height ) {
238+ state . offset = offset
239+ }
240+ this . setState ( state )
241+ }
242+
239243 loopJump = ( ) => {
240244 if ( ! this . state . loopJump ) return
241245 const i = this . state . index + ( this . props . loop ? 1 : 0 )
@@ -627,10 +631,7 @@ export default class extends Component {
627631 }
628632
629633 return (
630- < View style = { [ styles . container , {
631- width : state . width ,
632- height : state . height
633- } ] } >
634+ < View style = { [ styles . container , this . props . containerStyle ] } onLayout = { this . onLayout } >
634635 { this . renderScrollView ( pages ) }
635636 { props . showsPagination && ( props . renderPagination
636637 ? this . props . renderPagination ( state . index , state . total , this )
0 commit comments