You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: content/docs/refs-and-the-dom.md
+25-7
Original file line number
Diff line number
Diff line change
@@ -31,13 +31,13 @@ For example, instead of exposing `open()` and `close()` methods on a `Dialog` co
31
31
32
32
Your first inclination may be to use refs to "make things happen" in your app. If this is the case, take a moment and think more critically about where state should be owned in the component hierarchy. Often, it becomes clear that the proper place to "own" that state is at a higher level in the hierarchy. See the [Lifting State Up](/docs/lifting-state-up.html) guide for examples of this.
33
33
34
-
### Adding a Ref to a DOM Element
35
-
36
34
>**Note:**
37
35
>
38
36
>The examples below have updated to use the `React.createRef()` API introduced in React 16.3.
39
37
40
-
Refs can be created using `React.createRef()` and attached to React elements via the `ref` attribute. Refs are commonly assigned to an instance property when a component is constructed so they can be referenced throughout the the component.
38
+
### Creating Refs
39
+
40
+
Refs are created using `React.createRef()` and attached to React elements via the `ref` attribute. Refs are commonly assigned to an instance property when a component is constructed so they can be referenced throughout the the component.
41
41
42
42
```javascript{4,7}
43
43
class MyComponent extends React.Component {
@@ -51,7 +51,25 @@ class MyComponent extends React.Component {
51
51
}
52
52
```
53
53
54
-
When the `ref` attribute is used on an HTML element, the `ref` created in the constructor with `React.createRef()` receives the underlying DOM element as its `value` property. For example, this code uses a `ref` to store a reference to a DOM node:
54
+
### Accessing Refs
55
+
56
+
When a ref is passed to an element in `render`, a reference to the node becomes accessible at the `value` attribute of the ref.
57
+
58
+
```javascript
59
+
constnode=this.myRef.value
60
+
```
61
+
62
+
The value of the ref differs depending on whether the node is an HTML DOM node, a React class component instance, or a stateless functional component:
63
+
64
+
- When the `ref` attribute is used on an HTML element, the `ref` created in the constructor with `React.createRef()` receives the underlying DOM element as its `value` property.
65
+
- When the `ref` attribute is used on a custom component declared as a class, the `ref` object receives the mounted instance of the component as its `value`.
66
+
-**You may not use the `ref` attribute on functional components** because they don't have instances.
67
+
68
+
The examples below demonstrate the differences.
69
+
70
+
#### Adding a Ref to a DOM Element
71
+
72
+
This code uses a `ref` to store a reference to a DOM node:
55
73
56
74
```javascript{5,12,22}
57
75
class CustomTextInput extends React.Component {
@@ -89,9 +107,9 @@ class CustomTextInput extends React.Component {
89
107
90
108
React will assign the `value` property with the DOM element when the component mounts, and assign it back to `null` when it unmounts. `ref` updates happen before `componentDidMount` or `componentDidUpdate` lifecycle hooks.
91
109
92
-
### Adding a Ref to a Class Component
110
+
####Adding a Ref to a Class Component
93
111
94
-
When the `ref` attribute is used on a custom component declared as a class, the `ref` object receives the mounted instance of the component as its `value`. For example, if we wanted to wrap the `CustomTextInput` above to simulate it being clicked immediately after mounting:
112
+
If we wanted to wrap the `CustomTextInput` above to simulate it being clicked immediately after mounting, we could use a ref to get access to the custom input and call its `focusTextInput` method manually:
95
113
96
114
```javascript{4,7,12}
97
115
class AutoFocusTextInput extends React.Component {
@@ -120,7 +138,7 @@ class CustomTextInput extends React.Component {
120
138
}
121
139
```
122
140
123
-
### Refs and Functional Components
141
+
####Refs and Functional Components
124
142
125
143
**You may not use the `ref` attribute on functional components** because they don't have instances:
0 commit comments