@@ -59,14 +59,18 @@ const ScrollableTabView = React.createClass({
59
59
currentPage : this . props . initialPage ,
60
60
scrollValue : new Animated . Value ( this . props . initialPage ) ,
61
61
containerWidth : Dimensions . get ( 'window' ) . width ,
62
- sceneKeys : this . updateSceneKeys ( [ ] , this . props . initialPage ) ,
62
+ sceneKeys : this . newSceneKeys ( { currentPage : this . props . initialPage , } ) ,
63
63
} ;
64
64
} ,
65
65
66
66
componentWillReceiveProps ( props ) {
67
67
if ( props . page >= 0 && props . page !== this . state . currentPage ) {
68
68
this . goToPage ( props . page ) ;
69
69
}
70
+
71
+ if ( props . children !== this . props . children ) {
72
+ this . updateSceneKeys ( { page : this . state . currentPage , children : props . children , } ) ;
73
+ }
70
74
} ,
71
75
72
76
goToPage ( pageNumber ) {
@@ -87,12 +91,7 @@ const ScrollableTabView = React.createClass({
87
91
}
88
92
}
89
93
90
- if ( this . _children ( ) . length !== this . state . sceneKeys . length ) {
91
- let newKeys = this . updateSceneKeys ( this . state . sceneKeys , pageNumber ) ;
92
- this . setState ( { currentPage : pageNumber , sceneKeys : newKeys , } ) ;
93
- } else {
94
- this . setState ( { currentPage : pageNumber , } ) ;
95
- }
94
+ this . updateSceneKeys ( { page : pageNumber , } ) ;
96
95
} ,
97
96
98
97
renderTabBar ( props ) {
@@ -105,11 +104,16 @@ const ScrollableTabView = React.createClass({
105
104
}
106
105
} ,
107
106
108
- updateSceneKeys ( sceneKeys = [ ] , currentPage ) {
107
+ updateSceneKeys ( { page, children = this . props . children , callback = ( ) => { } , } ) {
108
+ let newKeys = this . newSceneKeys ( { previousKeys : this . state . sceneKeys , currentPage : page , children, } ) ;
109
+ this . setState ( { currentPage : page , sceneKeys : newKeys , } , callback ) ;
110
+ } ,
111
+
112
+ newSceneKeys ( { previousKeys = [ ] , currentPage = 0 , children = this . props . children , } ) {
109
113
let newKeys = [ ] ;
110
- this . _children ( ) . forEach ( ( child , idx ) => {
114
+ this . _children ( children ) . forEach ( ( child , idx ) => {
111
115
let key = this . _makeSceneKey ( child , idx ) ;
112
- if ( this . _keyExists ( sceneKeys , key ) || currentPage === idx ) {
116
+ if ( this . _keyExists ( previousKeys , key ) || currentPage === idx ) {
113
117
newKeys . push ( key ) ;
114
118
}
115
119
} ) ;
@@ -201,17 +205,11 @@ const ScrollableTabView = React.createClass({
201
205
if ( typeof localCurrentPage === 'object' ) {
202
206
localCurrentPage = currentPage . nativeEvent . position ;
203
207
}
204
- // scenekeys length and children length is same then no need to update the keys as all are stored by now
205
- if ( this . _children ( ) . length !== this . state . sceneKeys . length ) {
206
- let newKeys = this . updateSceneKeys ( this . state . sceneKeys , localCurrentPage ) ;
207
- this . setState ( { currentPage : localCurrentPage , sceneKeys : newKeys , } , ( ) => {
208
- this . props . onChangeTab ( { i : localCurrentPage , ref : this . _children ( ) [ localCurrentPage ] , } ) ;
209
- } ) ;
210
- } else {
211
- this . setState ( { currentPage : localCurrentPage , } , ( ) => {
212
- this . props . onChangeTab ( { i : localCurrentPage , ref : this . _children ( ) [ localCurrentPage ] , } ) ;
213
- } ) ;
214
- }
208
+
209
+ const callback = ( ) => {
210
+ this . props . onChangeTab ( { i : localCurrentPage , ref : this . _children ( ) [ localCurrentPage ] , } ) ;
211
+ } ;
212
+ this . updateSceneKeys ( { page : localCurrentPage , callback, } , ) ;
215
213
} ,
216
214
217
215
_updateScrollValue ( value ) {
@@ -230,8 +228,8 @@ const ScrollableTabView = React.createClass({
230
228
}
231
229
} ,
232
230
233
- _children ( ) {
234
- return React . Children . map ( this . props . children , ( child ) => child ) ;
231
+ _children ( children = this . props . children ) {
232
+ return React . Children . map ( children , ( child ) => child ) ;
235
233
} ,
236
234
237
235
render ( ) {
0 commit comments