gpt4 book ai didi

javascript - 使用 react refs 来关注兄弟元素

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

我正在尝试创建一个使用 refs 的 react 表单。我需要的是当用户在一个输入字段中输入一个值时。它应该自动关注下一个兄弟元素,即第二个输入元素等等,直到到达输入字段的末尾并且表单的值应该自动保存到状态中。

我对使用 react refs 很陌生。我尝试开发一个基本的工作,但陷入错误:

TypeError: Cannot read property 'nextSibling' of undefined
Todo._handleKeyPress
22 |
23 | e.preventDefault();
24 |
> 25 | let next = this.refs[field.name].nextSibling;
| ^ 26 | if (next && next.tagName === "INPUT") {
27 | this.refs[field.name].nextSibling.focus();
28 | }




import ReactDOM from "react-dom";
import React, { Component } from "react";
import "./style.css";

class Todo extends Component {
constructor(props) {
super(props); // create a ref to store the textInput DOM element

this.state= {

}


this.textInput1 = React.createRef();
this.textInput2 = React.createRef();
this.textInput3 = React.createRef();
}

_handleKeyPress = (e, field) => {
e.preventDefault();

let next = this.refs[field.name].nextSibling;
if (next && next.tagName === "INPUT") {
this.refs[field.name].nextSibling.focus();
}
};


submitForm = () => {

}


render() {
return (
<React.Fragment>
<form onSubmit={this.submitForm}>
<input
type="number"
name={this.textInput1}
maxLength="1"
ref={this.textInput1}
onKeyPress={e => this._handleKeyPress(e, this.textInput1)}
/>
<input
type="number"
name={this.textInput2}
maxLength="1"
ref={this.textInput3}
onKeyPress={e => this._handleKeyPress(e, this.textInput2)}
/>
<input
type="number"
name={this.textInput3}
maxLength="1"
ref={this.textInput3}
onKeyPress={e => this._handleKeyPress(e, this.textInput3)}
/>

<button>Submit</button>
</form>
</React.Fragment>
);
}
}

ReactDOM.render(<Todo />, document.getElementById("root"));

我在链接中包含了演示代码: https://stackblitz.com/edit/react-6gsfxd .任何形式的帮助表示赞赏。

最佳答案

第二个参数field已经是输入元素的引用,你不需要用 this.refs 调用它.

我对 _handleKeyPress 做了一些改动方法。

import ReactDOM from "react-dom";
import React, { Component } from "react";
import "./style.css";

class Todo extends Component {
constructor(props) {
super(props); // create a ref to store the textInput DOM element

this.state= {

}


this.textInput1 = React.createRef();
this.textInput2 = React.createRef();
this.textInput3 = React.createRef();
}

_handleKeyPress = (e, field) => {

let next = field.current.nextSibling;
field.current.nextSibling.focus();
};


submitForm = () => {

}


render() {
return (
<React.Fragment>
<form onSubmit={this.submitForm}>
<input
type="number"
name={this.textInput1}
maxLength="1"
ref={this.textInput1}
onKeyUp={e => this._handleKeyPress(e, this.textInput1)}
/>
<input
type="number"
name={this.textInput2}
maxLength="1"
ref={this.textInput2}
onKeyUp={e => this._handleKeyPress(e, this.textInput2)}
/>
<input
type="number"
name={this.textInput3}
maxLength="1"
ref={this.textInput3}
onKeyUp={e => this._handleKeyPress(e, this.textInput3)}
/>

<button>Submit</button>
</form>
</React.Fragment>
);
}
}

ReactDOM.render(<Todo />, document.getElementById("root"));


例子:

https://stackblitz.com/edit/react-fegfq3?file=index.js

关于javascript - 使用 react refs 来关注兄弟元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60128445/

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