Skip to content

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

@Wgy-yu

Description

@Wgy-yu

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions