Skip to content

Commit bbf80bc

Browse files
Drawer - Remove UNSAFE method (#945)
* Drawer - Swipeable - Remove 'UNSAFE_componentWillUpdate' method * remove android/gradlew.bat * remove expoDemo/android/gradlew.bat * removing android/gradlew.bat files * Replacing local variables with getters Co-authored-by: Ethan Sharabi <[email protected]>
1 parent 24d21db commit bbf80bc

File tree

1 file changed

+64
-50
lines changed

1 file changed

+64
-50
lines changed

src/components/drawer/Swipeable.tsx

Lines changed: 64 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ export default class Swipeable extends Component<PropType, StateType> {
102102
measureCompleted: false
103103
};
104104

105-
this._updateAnimatedEvent(props, this.state);
106-
107105
this._onGestureEvent = Animated.event([{nativeEvent: {translationX: dragX}}], {
108106
useNativeDriver: props.useNativeAnimations,
109107
listener: this._handleDrag
@@ -112,7 +110,7 @@ export default class Swipeable extends Component<PropType, StateType> {
112110

113111
_handleDrag = (e) => {
114112
const {onToggleSwipeLeft} = this.props;
115-
113+
116114
if (onToggleSwipeLeft) {
117115
// Drag left toggle
118116
const {rowWidth, leftWidth} = this.state;
@@ -123,7 +121,7 @@ export default class Swipeable extends Component<PropType, StateType> {
123121
// move item right
124122
this.dragThresholdReached = true;
125123
onToggleSwipeLeft({rowWidth, leftWidth, dragX: x, triggerHaptic: true});
126-
}
124+
}
127125
if (this.dragThresholdReached && x < threshold - 10) {
128126
// move item left
129127
this.dragThresholdReached = false;
@@ -132,27 +130,12 @@ export default class Swipeable extends Component<PropType, StateType> {
132130
}
133131
}
134132

135-
// TODO: change to componentDidUpdate
136-
UNSAFE_componentWillUpdate(props: PropType, state: StateType) {
137-
if (
138-
this.props.friction !== props.friction ||
139-
this.props.overshootLeft !== props.overshootLeft ||
140-
this.props.overshootRight !== props.overshootRight ||
141-
this.props.overshootFriction !== props.overshootFriction ||
142-
this.state.leftWidth !== state.leftWidth ||
143-
this.state.rightOffset !== state.rightOffset ||
144-
this.state.rowWidth !== state.rowWidth
145-
) {
146-
this._updateAnimatedEvent(props, state);
147-
}
148-
}
149-
150-
_updateAnimatedEvent = (props: PropType, state: StateType) => {
151-
const {friction, overshootFriction} = props;
152-
const {dragX, rowTranslation, leftWidth = 0, rowWidth = 0} = state;
153-
const {rightOffset = rowWidth} = state;
133+
getTransX = () => {
134+
const {friction, overshootFriction} = this.props;
135+
const {dragX, rowTranslation, leftWidth = 0, rowWidth = 0} = this.state;
136+
const {rightOffset = rowWidth} = this.state;
154137
const rightWidth = Math.max(0, rowWidth - rightOffset);
155-
const {overshootLeft = leftWidth > 0, overshootRight = rightWidth > 0} = props;
138+
const {overshootLeft = leftWidth > 0, overshootRight = rightWidth > 0} = this.props;
156139

157140
const transX = Animated.add(
158141
rowTranslation,
@@ -174,32 +157,63 @@ export default class Swipeable extends Component<PropType, StateType> {
174157
leftWidth + (overshootLeft || overshootFriction > 1 ? 1 : 0)
175158
],
176159
});
177-
this._transX = transX;
178-
this._showLeftAction =
179-
leftWidth > 0
180-
? transX.interpolate({
160+
161+
return transX;
162+
}
163+
164+
getShowLeftAction = () => {
165+
const transX = this.getTransX();
166+
const {leftWidth = 0} = this.state;
167+
168+
const showLeftAction = leftWidth > 0 ?
169+
transX.interpolate({
181170
inputRange: [-1, 0, leftWidth],
182171
outputRange: [0, 0, 1]
183172
})
184-
: new Animated.Value(0);
185-
this._leftActionTranslate = this._showLeftAction.interpolate({
173+
: new Animated.Value(0);
174+
175+
return showLeftAction;
176+
}
177+
178+
getLeftActionTranslate = () => {
179+
const showLeftAction = this.getShowLeftAction();
180+
181+
const leftActionTranslate = showLeftAction.interpolate({
186182
inputRange: [0, Number.MIN_VALUE],
187183
outputRange: [-10000, 0],
188184
extrapolate: 'clamp'
189185
});
190-
this._showRightAction =
191-
rightWidth > 0
192-
? transX.interpolate({
186+
187+
return leftActionTranslate;
188+
}
189+
190+
getShowRightAction = () => {
191+
const transX = this.getTransX();
192+
const {rowWidth = 0} = this.state;
193+
const {rightOffset = rowWidth} = this.state;
194+
const rightWidth = Math.max(0, rowWidth - rightOffset);
195+
196+
const showRightAction = rightWidth > 0 ?
197+
transX.interpolate({
193198
inputRange: [-rightWidth, 0, 1],
194199
outputRange: [1, 0, 0]
195200
})
196-
: new Animated.Value(0);
197-
this._rightActionTranslate = this._showRightAction.interpolate({
201+
: new Animated.Value(0);
202+
203+
return showRightAction;
204+
}
205+
206+
getRightActionTranslate = () => {
207+
const showRightAction = this.getShowRightAction();
208+
209+
const rightActionTranslate = showRightAction.interpolate({
198210
inputRange: [0, Number.MIN_VALUE],
199211
outputRange: [-10000, 0],
200212
extrapolate: 'clamp'
201213
});
202-
};
214+
215+
return rightActionTranslate;
216+
}
203217

204218
_onTapHandlerStateChange = ({nativeEvent}) => {
205219
if (this.rowState !== 0) {
@@ -225,13 +239,13 @@ export default class Swipeable extends Component<PropType, StateType> {
225239
const {rightOffset = rowWidth} = this.state;
226240
const rightWidth = rowWidth - rightOffset;
227241
const {
228-
fullSwipeLeft,
229-
fullSwipeRight,
230-
friction,
231-
leftThreshold = leftWidth / 2,
232-
rightThreshold = rightWidth / 2,
233-
fullLeftThreshold,
234-
fullRightThreshold,
242+
fullSwipeLeft,
243+
fullSwipeRight,
244+
friction,
245+
leftThreshold = leftWidth / 2,
246+
rightThreshold = rightWidth / 2,
247+
fullLeftThreshold,
248+
fullRightThreshold,
235249
onToggleSwipeLeft
236250
} = this.props;
237251
const startOffsetX = this._currentOffset() + dragX / friction;
@@ -291,7 +305,7 @@ export default class Swipeable extends Component<PropType, StateType> {
291305
dragX.setValue(0);
292306
rowTranslation.setValue(fromValue);
293307
this.rowState = Math.sign(toValue);
294-
308+
295309
Animated.spring(rowTranslation, {
296310
toValue,
297311
restSpeedThreshold: 1.7,
@@ -301,7 +315,7 @@ export default class Swipeable extends Component<PropType, StateType> {
301315
useNativeDriver: useNativeAnimations,
302316
...animationOptions
303317
}).start(({finished}) => {
304-
if (finished) {
318+
if (finished) {
305319
if (toValue === rowWidth && onFullSwipeLeft) {
306320
onFullSwipeLeft();
307321
} else if (toValue === -rowWidth && onFullSwipeRight) {
@@ -436,10 +450,10 @@ export default class Swipeable extends Component<PropType, StateType> {
436450
style={[
437451
styles.leftActions,
438452
leftActionsContainerStyle,
439-
{transform: [{translateX: this._leftActionTranslate}]}
453+
{transform: [{translateX: this.getLeftActionTranslate()}]}
440454
]}
441455
>
442-
{renderLeftActions(this._showLeftAction, this._transX)}
456+
{renderLeftActions(this.getShowLeftAction(), this.getTransX())}
443457
<View onLayout={this._onLeftLayout}/>
444458
</Animated.View>
445459
);
@@ -449,10 +463,10 @@ export default class Swipeable extends Component<PropType, StateType> {
449463
style={[
450464
styles.rightActions,
451465
rightActionsContainerStyle,
452-
{transform: [{translateX: this._rightActionTranslate}]}
466+
{transform: [{translateX: this.getRightActionTranslate()}]}
453467
]}
454468
>
455-
{renderRightActions(this._showRightAction, this._transX)}
469+
{renderRightActions(this.getShowRightAction(), this.getTransX())}
456470
<View onLayout={this._onRightLayout}/>
457471
</Animated.View>
458472
);
@@ -472,7 +486,7 @@ export default class Swipeable extends Component<PropType, StateType> {
472486
<Animated.View
473487
testID={testID}
474488
style={[
475-
{transform: [{translateX: this._transX}]},
489+
{transform: [{translateX: this.getTransX()}]},
476490
childrenContainerStyle
477491
]}
478492
>

0 commit comments

Comments
 (0)