Skip to content

Commit f545923

Browse files
committed
fix: useRef usage
1 parent 307e02a commit f545923

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/components/MagicCodeInput.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,20 @@ const inputPlaceholderSlots = Array.from(Array(CONST.MAGIC_CODE_LENGTH).keys());
8585

8686
function MagicCodeInput(props) {
8787
const inputRefs = useRef([]);
88-
8988
const [input, setInput] = useState('');
9089
const [focusedIndex, setFocusedIndex] = useState(0);
9190
const [editIndex, setEditIndex] = useState(0);
9291

9392
useImperativeHandle(props.innerRef, () => ({
9493
focus() {
9594
setFocusedIndex(0);
96-
inputRefs[0].focus();
95+
inputRefs.current[0].focus();
9796
},
9897
clear() {
9998
setInput('');
10099
setFocusedIndex(0);
101100
setEditIndex(0);
102-
inputRefs[0].focus();
101+
inputRefs.current[0].focus();
103102
props.onChangeText('');
104103
},
105104
}));
@@ -111,7 +110,7 @@ function MagicCodeInput(props) {
111110
if (!props.shouldSubmitOnComplete || _.filter(numbers, n => ValidationUtils.isNumeric(n)).length !== CONST.MAGIC_CODE_LENGTH) {
112111
return;
113112
}
114-
inputRefs[editIndex].blur();
113+
inputRefs.current[editIndex].blur();
115114
setFocusedIndex(undefined);
116115
props.onFulfill(props.value);
117116

@@ -126,10 +125,10 @@ function MagicCodeInput(props) {
126125

127126
let focusTimeout = null;
128127
if (props.shouldDelayFocus) {
129-
focusTimeout = setTimeout(() => inputRefs[0].focus(), CONST.ANIMATED_TRANSITION);
128+
focusTimeout = setTimeout(() => inputRefs.current[0].focus(), CONST.ANIMATED_TRANSITION);
130129
}
131130

132-
inputRefs[0].focus();
131+
inputRefs.current[0].focus();
133132

134133
return () => {
135134
if (!focusTimeout) {
@@ -249,20 +248,20 @@ function MagicCodeInput(props) {
249248
props.onChangeText(composeToString(numbers));
250249

251250
if (!_.isUndefined(newFocusedIndex)) {
252-
inputRefs[newFocusedIndex].focus();
251+
inputRefs.current[newFocusedIndex].focus();
253252
}
254253
} if (keyValue === 'ArrowLeft' && !_.isUndefined(focusedIndex)) {
255254
const newFocusedIndex = Math.max(0, focusedIndex - 1);
256255
setInput('');
257256
setFocusedIndex(newFocusedIndex);
258257
setEditIndex(newFocusedIndex);
259-
inputRefs[newFocusedIndex].focus();
258+
inputRefs.current[newFocusedIndex].focus();
260259
} else if (keyValue === 'ArrowRight' && !_.isUndefined(focusedIndex)) {
261260
const newFocusedIndex = Math.min(focusedIndex + 1, CONST.MAGIC_CODE_LENGTH - 1);
262261
setInput('');
263262
setFocusedIndex(newFocusedIndex);
264263
setEditIndex(newFocusedIndex);
265-
inputRefs[newFocusedIndex].focus();
264+
inputRefs.current[newFocusedIndex].focus();
266265
} else if (keyValue === 'Enter') {
267266
setInput('');
268267
props.onFulfill(props.value);
@@ -286,7 +285,7 @@ function MagicCodeInput(props) {
286285
</View>
287286
<View style={[StyleSheet.absoluteFillObject, styles.w100, styles.opacity0]}>
288287
<TextInput
289-
ref={ref => inputRefs[index] = ref}
288+
ref={ref => inputRefs.current[index] = ref}
290289
autoFocus={index === 0 && props.autoFocus}
291290
inputMode="numeric"
292291
textContentType="oneTimeCode"

0 commit comments

Comments
 (0)