forked from vasanthk/react-bits
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02.refs-over-findDOMNode.jsx
119 lines (104 loc) · 1.98 KB
/
02.refs-over-findDOMNode.jsx
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* Use findDOMNode() over callback refs
*
* Note:
* React also supports using a string (instead of a callback) as a ref prop on any component, although this approach is mostly legacy at this point.
* https://facebook.github.io/react/docs/more-about-refs.html
* http://stackoverflow.com/questions/37468913/why-ref-string-is-legacy
*
* @Reference:
* https://github.com/yannickcr/eslint-plugin-react/issues/678#issue-165177220
*/
/**
* findDOMNode(this)
*/
// Before:
class MyComponent extends Component {
componentDidMount() {
findDOMNode(this).scrollIntoView();
}
render() {
return <div />
}
}
// After:
class MyComponent extends Component {
componentDidMount() {
this.node.scrollIntoView();
}
render() {
return <div ref={node => this.node = node}/>
}
}
/**
* findDOMNode(stringDOMRef)
*/
// Before:
class MyComponent extends Component {
componentDidMount() {
findDOMNode(this.refs.something).scrollIntoView();
}
render() {
return (
<div>
<div ref='something'/>
</div>
)
}
}
// After:
class MyComponent extends Component {
componentDidMount() {
this.something.scrollIntoView();
}
render() {
return (
<div>
<div ref={node => this.something = node}/>
</div>
)
}
}
/**
* findDOMNode(childComponentStringRef)
*/
// Before:
class Field extends Component {
render() {
return <input type='text'/>
}
}
class MyComponent extends Component {
componentDidMount() {
findDOMNode(this.refs.myInput).focus();
}
render() {
return (
<div>
Hello,
<Field ref='myInput'/>
</div>
)
}
}
// After:
class Field extends Component {
render() {
return (
<input type='text' ref={this.props.inputRef}/>
)
}
}
class MyComponent extends Component {
componentDidMount() {
this.inputNode.focus();
}
render() {
return (
<div>
Hello,
<Field inputRef={node => this.inputNode = node}/>
</div>
)
}
}