react / react/react-native

TextInput value accumulates when using barcode scanner with rapid input (20ms interval) despite clear attempts in onSubmitEditing

Đang mở
#53,695 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Component: TextInput Needs: Triage :mag:
Ngôn ngữ chính
C++
Star
127k
Fork
25.3k
Merge trung bình
1 ngày 23 giờ
Pull request đã merge (30 ngày)
4

Mô tả

Description

Background
I'm implementing a barcode scanning feature using React Native's TextInput component. The barcode scanner emulates keyboard input and appends an Enter key press after each scan (triggering the onSubmitEditing event). Despite attempting to clear the input in onSubmitEditing using both state reset and ref.clear(), the TextInput retains the previous scan value during rapid scanning, resulting in concatenated strings.
Problem Description
When the barcode scanner inputs at an extremely high speed (with an approximate 20ms interval between inputs), the value from the previous scan persists in the TextInput and gets concatenated with the new scan value. For example:
First scan result: "841982400029557454"
Second scan expected result: "832904206576211315"
Actual result: "841982400029557454832904206576211315"
This issue occurs even though onSubmitEditing is triggered correctly and both clearing methods (state reset and ref manipulation) are invoked. The problem appears to be timing-related — the input event (onChangeText) for the next scan fires before the previous clear operation completes, which is particularly noticeable when the device's input interval is as low as 20ms.

Steps to reproduce

Reproduction Steps
Use the TextInput component with the specified props (see code snippet below)
Connect a barcode scanner with an input speed of approximately 20ms interval
Scan multiple barcodes consecutively
Observe console logs showing concatenated values in currentScannedText
Expected Behavior
Each barcode scan should completely replace the value in TextInput
onSubmitEditing should clear the input before processing the next scan's input
No value accumulation should occur regardless of scanning speed (even with input intervals as low as 20ms)

TextInput
ref={scanInputRef}
style={styles.scanInput}
value={scanInput}
autoComplete="off"
multiline={false}
onChangeText={text => {
if (!scanInputRef.current?.isFocused()) {
scanInputRef.current?.focus();
}
setScanInput(text);
}}
onKeyPress={e => {
console.log('e', e);
}}
placeholder="请扫描追溯码/发票"
// autoFocus
showSoftInputOnFocus={true}
blurOnSubmit={false}
autoCapitalize="none"
onSubmitEditing={e => {
const currentScannedText = e.nativeEvent.text;
console.log('currentScannedText', currentScannedText);
setScanInput('');
if (scanInputRef.current) {
scanInputRef.current.clear();
}

// e.preventDefault(); // Prevent default behavior (e.g., form submission)
// e.stopPropagation(); // Stop event bubbling
// handleScanSubmit(e); // Call your processing logic

}}
returnKeyType="done"
/>

React Native Version

0.80.1

Affected Platforms

Runtime - iOS, Runtime - Android

Output of npx @react-native-community/cli info
System:
  OS: Windows 11 10.0.26100
  CPU: "(16) x64 AMD Ryzen 7 260 w/ Radeon 780M Graphics        "
  Memory: 13.89 GB / 31.29 GB
Binaries:
  Node:
    version: 22.12.0
    path: C:\nvm4w\nodejs\node.EXE
  Yarn:
    version: 1.22.22
    path: C:\nvm4w\nodejs\yarn.CMD
  npm:
    version: 10.9.0
    path: C:\nvm4w\nodejs\npm.CMD
  Watchman: Not Found
SDKs:
  Android SDK: Not Found
  Windows SDK: Not Found
IDEs:
  Android Studio: AI-251.26094.121.2512.13930704
  Visual Studio: Not Found
Languages:
  Java: 17.0.12
  Ruby: Not Found
npmPackages:
  "@react-native-community/cli":
    installed: 19.1.1
  react-native:
    installed: 0.80.2
    wanted: ^0.80.2
  react-native-windows: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: Not found
  newArchEnabled: Not found
Stacktrace or Logs
currentScannedText 84406990002571708888
ScanInboundScreen.tsx:2299 currentScannedText 8440699000257170888884318380071678451839
ScanInboundScreen.tsx:2299 currentScannedText 844069900025717088888431838007167845183984213060006970260717
ScanInboundScreen.tsx:2299 currentScannedText 84406990002571708888843183800716784518398421306000697026071781555220308818745114
ScanInboundScreen.tsx:2299 currentScannedText 8440699000257170888884318380071678451839842130600069702607178155522030881874511481572430509845124269
ScanInboundScreen.tsx:2299 currentScannedText 844069900025717088888431838007167845183984213060006970260717815552203088187451148157243050984512426983191930004207372920
MANDATORY Reproducer

https://github.com/Wgy-yu/scan.git

Screenshots and Videos

No response

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với trình tái hiện bắt buộc và theo dõi chuỗi sự kiện onChangeText và onSubmitEditing của TextInput ở khoảng thời gian 20ms đã được báo cáo trên iOS và Android. Sử dụng các log của ScanInboundScreen.tsx quanh dòng 2299 để so sánh các lần quét liên tiếp; được coi là hoàn tất khi mỗi lần quét chỉ tạo ra giá trị của chính nó sau khi gửi, không bị tích lũy.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
react-native
Lĩnh vực
mobile
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Sôi nổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
52/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.