Skip to content

Commit 85d3ace

Browse files
authored
Merge pull request reactjs#2 from alexkrolick/create-ref-patch-2
Create "Accessing refs" section above specific examples
2 parents ffba37c + 5f30ab3 commit 85d3ace

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

content/docs/refs-and-the-dom.md

+25-7
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ For example, instead of exposing `open()` and `close()` methods on a `Dialog` co
3131

3232
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.
3333

34-
### Adding a Ref to a DOM Element
35-
3634
>**Note:**
3735
>
3836
>The examples below have updated to use the `React.createRef()` API introduced in React 16.3.
3937
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.
4141

4242
```javascript{4,7}
4343
class MyComponent extends React.Component {
@@ -51,7 +51,25 @@ class MyComponent extends React.Component {
5151
}
5252
```
5353

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+
const node = 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:
5573

5674
```javascript{5,12,22}
5775
class CustomTextInput extends React.Component {
@@ -89,9 +107,9 @@ class CustomTextInput extends React.Component {
89107

90108
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.
91109

92-
### Adding a Ref to a Class Component
110+
#### Adding a Ref to a Class Component
93111

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:
95113

96114
```javascript{4,7,12}
97115
class AutoFocusTextInput extends React.Component {
@@ -120,7 +138,7 @@ class CustomTextInput extends React.Component {
120138
}
121139
```
122140

123-
### Refs and Functional Components
141+
#### Refs and Functional Components
124142

125143
**You may not use the `ref` attribute on functional components** because they don't have instances:
126144

0 commit comments

Comments
 (0)