react / react/react-native

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

Open
#53,695 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Component: TextInput Needs: Triage :mag:
Dominant language
C++
Stars
127k
Forks
25.3k
Avg merge
1d 23h
Merged PRs (30d)
4

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the mandatory reproducer and trace TextInput's onChangeText and onSubmitEditing event sequence at the reported 20ms interval on iOS and Android. Use the ScanInboundScreen.tsx logs around line 2299 to compare successive scans; done means each scan produces only its own value after submission without accumulation.

Written by the indexing model from the issue text.

Assessment

Tech stack
react-native
Domain
mobile
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.