-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathindex.tsx
57 lines (48 loc) · 1.29 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import React, { Component } from 'react';
class Test extends Component {
private textInput: React.RefObject<any>;
constructor(parameters: { props: any }) {
const props = parameters.props;
super(props);
this.textInput = React.createRef();
}
componentDidMount() {
setTimeout(() => this.textInput.current.focusTextInput(), 2000);
}
render() {
return (
<CustomTextInput ref={this.textInput}/>
);
}
}
class CustomTextInput extends Component {
private textInput: React.RefObject<HTMLInputElement>;
constructor(props: any) {
super(props);
// 创建一个ref去储存textInput DOM元素
this.textInput = React.createRef();
this.focusTextInput = this.focusTextInput.bind(this);
}
focusTextInput() {
// 很明显的,让text input获得焦点使用了原生的DOM API
// 注意:我们通过current去获得DOM节点
this.textInput.current.focus();
}
render() {
// 告诉React我们想要将<input>的ref和构造器中创建的textInput联系起来
return (
<div>
<input
type="text"
ref={this.textInput}
/>
<input
type="button"
value="Focus the text input"
onClick={this.focusTextInput}
/>
</div>
);
}
}
export default Test;