Skip to content

Commit a1f8ae3

Browse files
Inbal-Tishethanshar
authored andcommitted
reducing onLayout renders from 3 to 1 (#477)
1 parent 786c846 commit a1f8ae3

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

src/interactableComponents/drawer/Swipeable.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@ export default class Swipeable extends Component<PropType, StateType> {
7878
this.state = {
7979
dragX,
8080
rowTranslation: new Animated.Value(0),
81+
rowWidth: Constants.screenWidth,
8182
leftWidth: undefined,
8283
rightOffset: undefined,
83-
rowWidth: Constants.screenWidth
84+
measureCompleted: false
8485
};
8586

8687
this._updateAnimatedEvent(props, this.state);
@@ -293,9 +294,36 @@ export default class Swipeable extends Component<PropType, StateType> {
293294
this._animateRow(this._currentOffset(), -rightWidth);
294295
};
295296

296-
_onRowLayout = ({nativeEvent}) => this.setState({rowWidth: nativeEvent.layout.width});
297-
_onLeftLayout = ({nativeEvent}) => this.setState({leftWidth: nativeEvent.layout.x});
298-
_onRightLayout = ({nativeEvent}) => this.setState({rightOffset: nativeEvent.layout.x});
297+
_onRowLayout = ({nativeEvent}) => this.handleMeasure('rowWidth', nativeEvent);
298+
_onLeftLayout = ({nativeEvent}) => this.handleMeasure('leftWidth', nativeEvent);
299+
_onRightLayout = ({nativeEvent}) => this.handleMeasure('rightOffset', nativeEvent);
300+
301+
handleMeasure = (name, nativeEvent) => {
302+
const {width, x} = nativeEvent.layout;
303+
304+
switch (name) {
305+
case 'rowWidth':
306+
this.rowWidth = width;
307+
break;
308+
case 'leftWidth':
309+
this.leftWidth = x;
310+
break;
311+
case 'rightOffset':
312+
this.rightOffset = x;
313+
break;
314+
default:
315+
break;
316+
}
317+
318+
if (this.rowWidth && this.leftWidth && this.rightOffset) {
319+
this.setState({
320+
rowWidth: this.rowWidth,
321+
leftWidth: this.leftWidth,
322+
rightOffset: this.rightOffset,
323+
measureCompleted: true
324+
});
325+
}
326+
};
299327

300328
render() {
301329
const {

0 commit comments

Comments
 (0)