Skip to content

Commit 9ab2a2b

Browse files
elicwhitefacebook-github-bot
authored andcommitted
Convert PanResponderExample to not use setNativeProps
Summary: Changing PanResponderExample to use React state instead of setNativeProps. Fabric likely won't have setNativeProps so this is a conversion in that direction. This is probably one of the most complicated usages of setNativeProps and it feels *okay* with React state. I'm sure it is worse on lower end devices though. In general we know that going to JS for an animation is not the correct approach so this getting worse in Fabric is probably fine for that reason too. Staying in native with something like react-native-gesture-handler is probably more aligned with the future. Changelog: [Internal] Reviewed By: lunaleaps Differential Revision: D18258130 fbshipit-source-id: eed6a8eb839c6607463f3140191945ea753a697b
1 parent 9ff090e commit 9ab2a2b

File tree

1 file changed

+50
-50
lines changed

1 file changed

+50
-50
lines changed

RNTester/js/examples/PanResponder/PanResponderExample.js

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
const React = require('react');
1414
const {PanResponder, StyleSheet, View} = require('react-native');
15+
const RNTesterPage = require('../../components/RNTesterPage');
1516

1617
import type {
1718
PanResponderInstance,
@@ -28,8 +29,24 @@ type CircleStyles = {
2829
const CIRCLE_SIZE = 80;
2930

3031
type Props = $ReadOnly<{||}>;
32+
type State = {|
33+
left: number,
34+
top: number,
35+
pressed: boolean,
36+
|};
37+
38+
class PanResponderExample extends React.Component<Props, State> {
39+
_previousLeft: number = 20;
40+
_previousTop: number = 84;
41+
_circleStyles: {|style: CircleStyles|} = {style: {}};
42+
circle: ?React.ElementRef<typeof View> = null;
43+
44+
state: State = {
45+
left: 20,
46+
top: 84,
47+
pressed: false,
48+
};
3149

32-
class PanResponderExample extends React.Component<Props> {
3350
_handleStartShouldSetPanResponder = (
3451
event: PressEvent,
3552
gestureState: GestureState,
@@ -50,17 +67,22 @@ class PanResponderExample extends React.Component<Props> {
5067
event: PressEvent,
5168
gestureState: GestureState,
5269
) => {
53-
this._highlight();
70+
this.setState({
71+
pressed: true,
72+
});
5473
};
5574

5675
_handlePanResponderMove = (event: PressEvent, gestureState: GestureState) => {
57-
this._circleStyles.style.left = this._previousLeft + gestureState.dx;
58-
this._circleStyles.style.top = this._previousTop + gestureState.dy;
59-
this._updateNativeStyles();
76+
this.setState({
77+
left: this._previousLeft + gestureState.dx,
78+
top: this._previousTop + gestureState.dy,
79+
});
6080
};
6181

6282
_handlePanResponderEnd = (event: PressEvent, gestureState: GestureState) => {
63-
this._unHighlight();
83+
this.setState({
84+
pressed: false,
85+
});
6486
this._previousLeft += gestureState.dx;
6587
this._previousTop += gestureState.dy;
6688
};
@@ -74,52 +96,29 @@ class PanResponderExample extends React.Component<Props> {
7496
onPanResponderTerminate: this._handlePanResponderEnd,
7597
});
7698

77-
_previousLeft: number = 0;
78-
_previousTop: number = 0;
79-
_circleStyles: {|style: CircleStyles|} = {style: {}};
80-
circle: ?React.ElementRef<typeof View> = null;
81-
82-
UNSAFE_componentWillMount() {
83-
this._previousLeft = 20;
84-
this._previousTop = 84;
85-
this._circleStyles = {
86-
style: {
87-
left: this._previousLeft,
88-
top: this._previousTop,
89-
backgroundColor: 'green',
90-
},
91-
};
92-
}
93-
94-
componentDidMount() {
95-
this._updateNativeStyles();
96-
}
97-
98-
_highlight() {
99-
this._circleStyles.style.backgroundColor = 'blue';
100-
this._updateNativeStyles();
101-
}
102-
103-
_unHighlight() {
104-
this._circleStyles.style.backgroundColor = 'green';
105-
this._updateNativeStyles();
106-
}
107-
108-
_updateNativeStyles() {
109-
this.circle && this.circle.setNativeProps(this._circleStyles);
110-
}
111-
11299
render(): React.Node {
113100
return (
114-
<View style={styles.container}>
115-
<View
116-
ref={circle => {
117-
this.circle = circle;
118-
}}
119-
style={styles.circle}
120-
{...this._panResponder.panHandlers}
121-
/>
122-
</View>
101+
<RNTesterPage
102+
noSpacer={true}
103+
noScroll={true}
104+
title="Basic gesture handling">
105+
<View style={styles.container}>
106+
<View
107+
ref={circle => {
108+
this.circle = circle;
109+
}}
110+
style={[
111+
styles.circle,
112+
{
113+
translateX: this.state.left,
114+
translateY: this.state.top,
115+
backgroundColor: this.state.pressed ? 'blue' : 'green',
116+
},
117+
]}
118+
{...this._panResponder.panHandlers}
119+
/>
120+
</View>
121+
</RNTesterPage>
123122
);
124123
}
125124
}
@@ -143,6 +142,7 @@ const styles = StyleSheet.create({
143142
exports.title = 'PanResponder Sample';
144143
exports.description =
145144
'Shows the Use of PanResponder to provide basic gesture handling';
145+
exports.simpleExampleContainer = true;
146146
exports.examples = [
147147
{
148148
title: 'Basic gesture handling',

0 commit comments

Comments
 (0)