Skip to content

Commit 9f90bf4

Browse files
committed
improve TabController page performance
1 parent e87d7ab commit 9f90bf4

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

src/incubator/TabController/TabPage.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import React, {Component} from 'react';
1+
import React, {PureComponent} from 'react';
22
import {StyleSheet} from 'react-native';
33
import PropTypes from 'prop-types';
44
import Reanimated from 'react-native-reanimated';
55
import TabBarContext from './TabBarContext';
66

7-
const {cond, and, call, block, eq} = Reanimated;
7+
const {Code, Value, cond, set, and, call, block, eq} = Reanimated;
88

99
/**
10-
* @description: TabController's TabPage
11-
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/incubatorScreens/TabControllerScreen/index.js
12-
*/
13-
export default class TabPage extends Component {
10+
* @description: TabController's TabPage
11+
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/incubatorScreens/TabControllerScreen/index.js
12+
*/
13+
export default class TabPage extends PureComponent {
1414
static displayName = 'TabController.TabPage';
1515

1616
static contextType = TabBarContext;
@@ -34,6 +34,10 @@ export default class TabPage extends Component {
3434
loaded: !this.props.lazy,
3535
};
3636

37+
_opacity = new Value(0);
38+
_zIndex = new Value(0);
39+
_pageStyle = [styles.page, {opacity: this._opacity}, {zIndex: this._zIndex}];
40+
3741
lazyLoad = () => {
3842
this.setState({
3943
loaded: true,
@@ -44,16 +48,22 @@ export default class TabPage extends Component {
4448
const {currentPage} = this.context;
4549
const {index, lazy, testID} = this.props;
4650
const {loaded} = this.state;
47-
const opacity = block([
48-
cond(and(eq(currentPage, index), lazy, !loaded), call([], this.lazyLoad)),
49-
cond(eq(currentPage, index), 1, 0),
50-
]);
51-
52-
const zIndex = cond(eq(currentPage, index), 1, 0);
5351

5452
return (
55-
<Reanimated.View style={[styles.page, {opacity}, {zIndex}]} testID={testID}>
53+
<Reanimated.View style={this._pageStyle} testID={testID}>
5654
{loaded && this.props.children}
55+
<Code>
56+
{() => {
57+
return block([
58+
cond(and(eq(currentPage, index), lazy, !loaded), call([], this.lazyLoad)),
59+
cond(
60+
eq(currentPage, index),
61+
[set(this._opacity, 1), set(this._zIndex, 1)],
62+
[set(this._opacity, 0), set(this._zIndex, 0)],
63+
),
64+
]);
65+
}}
66+
</Code>
5767
</Reanimated.View>
5868
);
5969
}

src/incubator/TabController/index.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@ class TabController extends Component {
6969
this.setState({itemStates, ignoredItems});
7070
};
7171

72-
onChangeIndex = () => {
73-
_.invoke(this.props, 'onChangeIndex', this.props);
74-
};
75-
7672
render() {
7773
const {itemStates, ignoredItems} = this.state;
7874
return (

0 commit comments

Comments
 (0)