
在React中开发类似OTP输入框的组件时,我们通常会创建多个独立的 <input> 元素,并需要对每个输入框进行精细的控制,例如输入校验、自动聚焦到下一个输入框或在退格时聚焦到上一个输入框。当尝试通过原生DOM事件监听器(addEventListener)来处理这些交互时,可能会遇到 Cannot read properties of undefined (reading 'value') 这样的错误。
这个错误通常发生在尝试访问一个未定义对象的属性时。在给定的场景中,它指向的是在事件处理函数中尝试访问 e.target.value 时,e 变量本身是 undefined 或不是预期的事件对象。
错误根源分析:addEventListener 与 bind 的参数传递机制
问题的核心在于 addEventListener 如何将事件对象传递给其监听器,以及 Function.prototype.bind() 方法如何预设参数。
考虑以下代码片段:
// 原始的事件处理函数定义
const handleInput = (e, index) => {
// ... 逻辑,期望 e 是事件对象,index 是索引
const isValid = expression.test(e.target.value); // 错误发生在这里
};
// 事件监听器注册
inpRef.current.forEach((input, index) => {
input.addEventListener('input', handleInput.bind(null, index));
});当 input.addEventListener('input', ...) 触发时,浏览器会将一个 Event 对象作为第一个参数传递给注册的监听器函数。然而,在这里我们使用了 handleInput.bind(null, index)。
bind() 方法的作用是创建一个新的函数,当这个新函数被调用时,其 this 关键字会被设置为 bind() 的第一个参数(这里是 null),并且预设 bind() 的后续参数。因此,handleInput.bind(null, index) 会生成一个新函数,当它被调用时,index 会作为它的第一个参数传递。
所以,当实际的 input 事件发生时:
这意味着,在 handleInput(e, index) 函数内部:
因此,当代码执行 e.target.value 时,实际上是在尝试访问一个数字类型(index 值)的 target 属性,这自然会导致 undefined 错误。
解决这个问题的关键是调整 handleInput 函数的参数顺序,使其与 bind 方法和 addEventListener 的实际参数传递顺序相匹配。
修改前:
const handleInput = (e, index) => { /* ... */ };
// 绑定时:handleInput.bind(null, index)
// 实际接收:e = index值, index = Event对象修改后:
const handleInput = (index, e) => {
// ... 逻辑,现在 index 是索引,e 是事件对象
const current = inpRef.current[index];
// ...
const isValid = expression.test(e.target.value); // 现在 e 是事件对象,可以正确访问 target.value
// ...
};
// 绑定时保持不变:handleInput.bind(null, index)
// 实际接收:index = index值, e = Event对象通过将 handleInput 的参数顺序改为 (index, e),index 参数将正确接收到 bind 预设的索引值,而 e 参数将正确接收到 addEventListener 提供的事件对象。这样,e.target.value 就能正常访问了。
除了修复上述参数顺序问题,一个完整的OTP输入组件还需要处理多种用户交互,以提供流畅的用户体验。下面我们将构建一个更完善的组件。
我们将使用 useState 来管理OTP的各个数字,并通过 useRef 来引用每个输入框,以便进行聚焦控制。
import { useState, useEffect, useRef, useCallback } from 'react';
import '../src/component.css'; // 假设有基本的样式
export default function OtpInputComponent() {
// 使用 useState 存储 OTP 数组,初始化为6个空字符串
const [otp, setOtp] = useState(new Array(6).fill(''));
// 使用 useRef 管理所有 input 元素的引用
const inputRefs = useRef([]);
// 用于收集完整的 OTP 字符串
const fullOtp = otp.join('');
// 模拟一个计时器,与OTP功能无关,仅为展示组件生命周期
const [count, setCount] = useState(0);
useEffect(() => {
const timer = setTimeout(() => {
setCount(prevCount => prevCount + 1);
}, 1000);
return () => clearTimeout(timer);
}, [count]);
// 处理单个输入框的输入事件
const handleInputChange = useCallback((index, event) => {
const { value } = event.target;
const currentInput = inputRefs.current[index];
// 1. 验证输入:只允许单个数字
const isValidDigit = /^\d$/.test(value);
if (!isValidDigit && value !== '') {
// 如果输入非数字或多于一位,清空当前输入
currentInput.value = '';
return;
}
// 2. 更新 OTP 状态
const newOtp = [...otp];
newOtp[index] = value;
setOtp(newOtp);
// 3. 自动聚焦到下一个输入框
if (value && index < otp.length - 1) {
inputRefs.current[index + 1]?.focus();
}
}, [otp]); // 依赖 otp 状态,确保获取到最新的值
// 处理键盘事件(如退格、删除、方向键)
const handleKeyDown = useCallback((index, event) => {
const { key } = event;
const currentInput = inputRefs.current[index];
if (key === 'Backspace') {
// 如果当前输入框有值,清空当前输入框
if (currentInput.value) {
event.preventDefault(); // 阻止默认的退格行为
const newOtp = [...otp];
newOtp[index] = '';
setOtp(newOtp);
} else if (index > 0) {
// 如果当前输入框无值且不是第一个,聚焦到上一个输入框并清空其内容
event.preventDefault(); // 阻止默认的退格行为
inputRefs.current[index - 1]?.focus();
const newOtp = [...otp];
newOtp[index - 1] = '';
setOtp(newOtp);
}
} else if (key === 'Delete') {
// 如果当前输入框有值,清空当前输入框
if (currentInput.value) {
event.preventDefault();
const newOtp = [...otp];
newOtp[index] = '';
setOtp(newOtp);
} else if (index < otp.length - 1) {
// 如果当前输入框无值且不是最后一个,聚焦到下一个输入框
event.preventDefault();
inputRefs.current[index + 1]?.focus();
}
} else if (key === 'ArrowLeft' && index > 0) {
inputRefs.current[index - 1]?.focus();
} else if (key === 'ArrowRight' && index < otp.length - 1) {
inputRefs.current[index + 1]?.focus();
}
}, [otp]);
// 处理粘贴事件
const handlePaste = useCallback((index, event) => {
event.preventDefault(); // 阻止默认粘贴行为
const pasteData = event.clipboardData.getData('text').trim();
// 提取纯数字,并限制长度不超过剩余的输入框数量
const digits = pasteData.replace(/\D/g, '').slice(0, otp.length - index);
if (digits.length > 0) {
const newOtp = [...otp];
for (let i = 0; i < digits.length; i++) {
if (index + i < otp.length) {
newOtp[index + i] = digits[i];
}
}
setOtp(newOtp);
// 粘贴后聚焦到最后一个粘贴的输入框或下一个输入框
const lastPastedIndex = index + digits.length - 1;
if (lastPastedIndex < otp.length - 1) {
inputRefs.current[lastPastedIndex + 1]?.focus();
} else {
inputRefs.current[otp.length - 1]?.focus(); // 聚焦到最后一个输入框
}
}
}, [otp]);
// 初始聚焦到第一个输入框
useEffect(() => {
inputRefs.current[0]?.focus();
}, []);
return (
<>
<p>Counter: {count}</p>
<h4>Now enter the OTP</h4>
<div className="whole">
<h5>Send the OTP to your phone Number</h5>
<div className="container">
{otp.map((digit, index) => (
<input
key={index}
className="text-field"
type="text" // 使用 text 类型以便更好地控制输入,并通过 pattern 限制
inputMode="numeric" // 提示移动设备键盘类型
pattern="[0-9]*" // 浏览器层面的数字输入限制
maxLength="1" // 限制单个输入框只能输入一个字符
value={digit} // 受控组件
onChange={(e) => handleInputChange(index, e)} // 使用 onChange 替代 onInput
onKeyDown={(e) => handleKeyDown(index, e)}
onPaste={(e) => handlePaste(index, e)}
ref={(el) => (inputRefs.current[index] = el)}
/>
))}
</div>
<button className="btn" onClick={() => console.log("OTP Submitted:", fullOtp)}>
SUBMIT
</button>
<p>Current OTP: {fullOtp}</p>
</div>
</>
);
}通过上述方法,我们可以构建一个功能强大、用户友好的React OTP输入组件,避免常见的错误,并提供流畅的交互体验。
以上就是构建高效安全的React OTP输入组件:深度解析与实现的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号