【发布时间】:2021-08-19 06:53:40
【问题描述】:
我在地图中使用多个输入,当我在 React Hooks 中单击输入时,我想将焦点设置为下一个输入。 在参考文献的帮助下
我正在使用材质 ui 文本字段来获取输入
我尝试在没有 ref 的 react 类组件中工作,但在钩子中它不起作用 类组件代码:
constructor(props) {
this.state = {}
}
inputRefs = [];
_handleKeyPress = e => {
const {currentTarget} = e;
let inputindex = this.inputRefs.indexOf(currentTarget)
if (inputindex < this.inputRefs.length - 1) {
this.inputRefs[inputindex + 1].focus()
}
else {
this.inputRefs[0].focus()
}
};
Inside render in 在地图函数中添加了这个
this.state.data.map((data) => return (
<TextField
inputProps = {{onKeyPress:(e) => this.function1(e, data)}}
onChange={this.changevaluefunction}
inputRef={ref => this.inputRefs.push(ref)}
onFocus={this.handleFocus} ref={`input${id}`} /> ))
【问题讨论】:
-
你使用的是旧的 React ref 语法。使用
createRef创建一个 React 引用数组,并在尝试聚焦时访问当前值。能否提供更全面的组件代码示例? stackoverflow.com/help/minimal-reproducible-example -
是的,我当然可以更新问题
标签: javascript node.js reactjs react-hooks