Skip to content

Commit 9a17ce8

Browse files
authored
Merge pull request ptomasroos#340 from skv-headless/refactoring-lazy
Refactoring lazy
2 parents 10f1b6b + 7e6f245 commit 9a17ce8

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

index.js

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,18 @@ const ScrollableTabView = React.createClass({
5959
currentPage: this.props.initialPage,
6060
scrollValue: new Animated.Value(this.props.initialPage),
6161
containerWidth: Dimensions.get('window').width,
62-
sceneKeys: this.updateSceneKeys([], this.props.initialPage),
62+
sceneKeys: this.newSceneKeys({ currentPage: this.props.initialPage, }),
6363
};
6464
},
6565

6666
componentWillReceiveProps(props) {
6767
if (props.page >= 0 && props.page !== this.state.currentPage) {
6868
this.goToPage(props.page);
6969
}
70+
71+
if (props.children !== this.props.children) {
72+
this.updateSceneKeys({ page: this.state.currentPage, children: props.children, });
73+
}
7074
},
7175

7276
goToPage(pageNumber) {
@@ -87,12 +91,7 @@ const ScrollableTabView = React.createClass({
8791
}
8892
}
8993

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, });
9695
},
9796

9897
renderTabBar(props) {
@@ -105,11 +104,16 @@ const ScrollableTabView = React.createClass({
105104
}
106105
},
107106

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, }) {
109113
let newKeys = [];
110-
this._children().forEach((child, idx) => {
114+
this._children(children).forEach((child, idx) => {
111115
let key = this._makeSceneKey(child, idx);
112-
if (this._keyExists(sceneKeys, key) || currentPage === idx) {
116+
if (this._keyExists(previousKeys, key) || currentPage === idx) {
113117
newKeys.push(key);
114118
}
115119
});
@@ -201,17 +205,11 @@ const ScrollableTabView = React.createClass({
201205
if (typeof localCurrentPage === 'object') {
202206
localCurrentPage = currentPage.nativeEvent.position;
203207
}
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, }, );
215213
},
216214

217215
_updateScrollValue(value) {
@@ -230,8 +228,8 @@ const ScrollableTabView = React.createClass({
230228
}
231229
},
232230

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);
235233
},
236234

237235
render() {

0 commit comments

Comments
 (0)