react / react/react-native

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

未关闭
#53,695 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Component: TextInput Needs: Triage :mag:
主要语言
C++
星标
127k
派生
25.3k
平均合并
1 天 23 小时
30 天内合并 PR
4

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从必需的复现步骤开始,在 iOS 和 Android 上按照报告的 20ms 间隔跟踪 TextInput 的 onChangeText 和 onSubmitEditing 事件序列。使用 ScanInboundScreen.tsx 第 2299 行附近的日志比较连续扫描;完成标准是每次扫描在提交后只产生自身的值,不发生累积。

由索引模型根据 Issue 内容生成。

评估

技术栈
react-native
领域
mobile
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
活跃
描述清晰度
基本清楚
新手友好度
52/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。