- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图让每个字母输入时焦点移动到下一个输入的组件。
我想我需要多个 ref
像一个数组,但我不知道它。
这是问题的示例代码。
function PIN({length, onChange, value}){
const inputEl = React.useRef(null);
function handleChange(e){
onChange(e);
inputEl.current.focus();
}
return (
<div>
{
new Array(length).fill(0).map((i)=>(
<input type="text" ref={inputEl} onChange={handleChange} />
))
}
</div>
)
}
最佳答案
您可以创建多个引用
function PIN({length, onChange, value}){
const inputRefs = useMemo(() => Array(length).fill(0).map(i=> React.createRef()), []);
const handleChange = index => (e) => {
//onChange(e); // don't know about the logic of this onChange if you have multiple inputs
if (inputRefs[index + 1]) inputRefs[index + 1].current.focus();
}
return (
<div>
{
new Array(length).fill(0).map((inp, index)=>(
<input type="text" ref={inputRefs[index]} onChange={handleChange(index)} />
))
}
</div>
)
}
关于reactjs - 将 useRef 与 Array react 为动态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61245376/
我有多个按钮,单击它们会触发不同的音频文件。 我设法为每个按钮使用不同的 useRef 使其工作,但我想有更好的方法来实现这一点: const App = () => { const myAudi
我有多个按钮,单击它们会触发不同的音频文件。 我设法为每个按钮使用不同的 useRef 使其工作,但我想有更好的方法来实现这一点: const App = () => { const myAudi
我正在阅读有关 React 中的钩子(Hook)的内容,但我在理解 useRef 和 useCallback 钩子(Hook)之间的区别时遇到了一些困难。 具体来说,我希望了解如何使用这两者来避免子组
我正在尝试使用 Element.getBoundingClientRect() 来获取 dom 元素的 x、y、top、left 值。 const editorRef = useRef() ... /
NX构建在本地成功,但在Amplify上失败,错误如下:。即使我没有在代码中的任何地方使用useRef,这种情况也会发生。。我尝试添加一个error.tsx文件,因为错误表明它们来自预渲染/404和/
问题是 useRef 在第一次渲染时被触发。这可能是一个问题的两个例子。 When one can have some loading const Problem1 = () => { const
查看 hooks 文档和一些博客我们了解到,在使用 useRef() 时,我们存储了一个可变值。 Docs : You might be familiar with refs primarily as
我对下面使用 useRef 存储以前的状态值感到困惑。本质上,它如何能够正确显示先前的值。由于 useEffect 依赖于“value”,我的理解是每次“value”更改时(即当用户更新文本框时),它
当我编写此代码时,出现错误: React Hook "useRef" cannot be called inside a callback. React Hooks must be called in
我想知道这两个代码之间的区别 1: import React from "react"; import ReactDOM from "react-dom"; function App() { co
我有以下组件: const ParentComponent: React.FC = () => { const networkRef: any = useRef(); // Somew
转向功能性 React 组件,useRef 似乎是镜像类级别变量的方式。考虑: class Component extends React.Component { private readon
我有以下组件: // component.js import React from 'react'; import useComponentSize from 'useComponentSize';
我正在尝试设置为使用 useRef设置 div 的 ref 的钩子(Hook),但似乎一旦找到 ref 值就不会更新。下面怎么可能? const SelectDropDown = props
今天使用 react 的 ref 可能有点令人困惑。 回到类组件的时代,文档中非常清楚。 我们应该将 refs 主要用于 DOM 元素 : https://reactjs.org/docs/refs-
人们使用 useRef 来保存最新的值,如此代码 function MyComponent({ value }) { const valueRef = useRef(); useEffect(
我使用 React 的 useRef 钩子(Hook)捕获了一个元素。 如果我使用 console.log(this.inputRef) 我得到: 有没有办法使用 this.inputRef 更改该
我想使用 useRef Hook 来更改 DOM 元素的样式: const Box = props => { const box = useRef(0); const onClick = ()
我已阅读A Complete Guide to useEffect - Swimming Against the Tide react 过度。 这个例子表明,如果我们想要获取最新的count,我们可以
继续我的最后一个问题here ,我一直在尝试将引用映射到其他路线。滚动处理程序正在工作,但 ref.current 为 null。所以我正在寻找这个困境的答案。 不使用外部依赖项,如何解决此问题? 应
我是一名优秀的程序员,十分优秀!