gpt4 book ai didi

javascript - ref.current.focus() 不是函数

转载 作者:行者123 更新时间:2023-12-04 13:12:16 25 4
gpt4 key购买 nike

我试图在 onBlur 时使用 ref 来关注元素。我正在使用 react v.16.9.0
我的代码是这样的:

const handleKeyDown = (e, element, index) => {
element.current.focus();
event.preventDefault();
};

const pinDigitBuilder = () => {
const arr = Array(...Array(TOTAL_PIN_DIGITS));
return arr.map((x, i) => {
const inputRef = useRef(null);
console.log(inputRef);
return (
<Field
key={`${PIN_DIGIT}${i + 1}`}
id={`${PIN_DIGIT}${i + 1}`}
name={`${PIN_DIGIT}${i + 1}`}
ref={inputRef}
component={InputTextField}
className="text xxs2"
classNameInvalid="text xxs2 error"
type="text"
divClassName="fieldWrap"
maxLength={1}
normalize={keepNumbersOnly}
errorStyle={{ display: 'none' }}
autoComplete="off"
onBlur={(e) => { handleKeyDown(e, inputRef, i); }}
/>
);
});
};
当我在该字段外单击时,发生错误说 element.current.focus();。我不明白。我按照文档中的详细说明使用 useRef,但我不知道我缺少什么。有任何想法吗?

最佳答案

我假设 <Field />是一个功能组件。里面<Field />组件有一个输入元素。如果是这种情况,那么您必须使用 forwardref将引用传递给 child 。这是引用链接
https://reactjs.org/docs/forwarding-refs.html .

关于javascript - ref.current.focus() 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63995732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com