Skip to content

Commit c5e7190

Browse files
authored
[react-interactions] Press with useRef instead of useState (#16870)
We only need to read and modify the value for the lifetime of the hook
1 parent 911104a commit c5e7190

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

packages/react-interactions/events/src/dom/Press.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ export function usePress(props: PressProps) {
128128
onPressStart,
129129
} = safeProps;
130130

131-
const [active, updateActive] = React.useState(null);
131+
const activeResponder = React.useRef(null);
132132

133133
const tap = useTap({
134-
disabled: disabled || active === 'keyboard',
134+
disabled: disabled || activeResponder.current === 'keyboard',
135135
preventDefault,
136136
onAuxiliaryTap(e) {
137137
if (onPressStart != null) {
@@ -147,56 +147,56 @@ export function usePress(props: PressProps) {
147147
}
148148
},
149149
onTapStart(e) {
150-
if (active == null) {
151-
updateActive('tap');
150+
if (activeResponder.current == null) {
151+
activeResponder.current = 'tap';
152152
if (onPressStart != null) {
153153
onPressStart(createGestureState(e, 'pressstart'));
154154
}
155155
}
156156
},
157157
onTapChange: onPressChange,
158158
onTapUpdate(e) {
159-
if (active === 'tap') {
159+
if (activeResponder.current === 'tap') {
160160
if (onPressMove != null) {
161161
onPressMove(createGestureState(e, 'pressmove'));
162162
}
163163
}
164164
},
165165
onTapEnd(e) {
166-
if (active === 'tap') {
166+
if (activeResponder.current === 'tap') {
167167
if (onPressEnd != null) {
168168
onPressEnd(createGestureState(e, 'pressend'));
169169
}
170170
if (onPress != null) {
171171
onPress(createGestureState(e, 'press'));
172172
}
173-
updateActive(null);
173+
activeResponder.current = null;
174174
}
175175
},
176176
onTapCancel(e) {
177-
if (active === 'tap') {
177+
if (activeResponder.current === 'tap') {
178178
if (onPressEnd != null) {
179179
onPressEnd(createGestureState(e, 'pressend'));
180180
}
181-
updateActive(null);
181+
activeResponder.current = null;
182182
}
183183
},
184184
});
185185

186186
const keyboard = useKeyboard({
187-
disabled: disabled || active === 'tap',
187+
disabled: disabled || activeResponder.current === 'tap',
188188
onClick(e) {
189189
if (preventDefault !== false) {
190190
e.preventDefault();
191191
}
192-
if (active == null && onPress != null) {
192+
if (activeResponder.current == null && onPress != null) {
193193
onPress(createGestureState(e, 'press'));
194194
}
195195
},
196196
onKeyDown(e) {
197-
if (active == null && isValidKey(e)) {
197+
if (activeResponder.current == null && isValidKey(e)) {
198198
handlePreventDefault(preventDefault, e);
199-
updateActive('keyboard');
199+
activeResponder.current = 'keyboard';
200200

201201
if (onPressStart != null) {
202202
onPressStart(createGestureState(e, 'pressstart'));
@@ -207,7 +207,7 @@ export function usePress(props: PressProps) {
207207
}
208208
},
209209
onKeyUp(e) {
210-
if (active === 'keyboard' && isValidKey(e)) {
210+
if (activeResponder.current === 'keyboard' && isValidKey(e)) {
211211
handlePreventDefault(preventDefault, e);
212212
if (onPressChange != null) {
213213
onPressChange(false);
@@ -218,7 +218,7 @@ export function usePress(props: PressProps) {
218218
if (onPress != null) {
219219
onPress(createGestureState(e, 'press'));
220220
}
221-
updateActive(null);
221+
activeResponder.current = null;
222222
}
223223
},
224224
});

packages/react-interactions/events/src/dom/testing-library/domEvents.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function createPointerEvent(
104104
ctrlKey,
105105
detail,
106106
getModifierState(keyArg) {
107-
createGetModifierState(keyArg, modifierState);
107+
return createGetModifierState(keyArg, modifierState);
108108
},
109109
height: isMouse ? 1 : height != null ? height : defaultPointerSize,
110110
metaKey,
@@ -150,7 +150,7 @@ function createKeyboardEvent(
150150
altKey,
151151
ctrlKey,
152152
getModifierState(keyArg) {
153-
createGetModifierState(keyArg, modifierState);
153+
return createGetModifierState(keyArg, modifierState);
154154
},
155155
isComposing,
156156
key,
@@ -193,7 +193,7 @@ function createMouseEvent(
193193
ctrlKey,
194194
detail,
195195
getModifierState(keyArg) {
196-
createGetModifierState(keyArg, modifierState);
196+
return createGetModifierState(keyArg, modifierState);
197197
},
198198
metaKey,
199199
movementX,

0 commit comments

Comments
 (0)