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
+12-8Lines changed: 12 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ redirect_from:
11
11
permalink: docs/refs-and-the-dom.html
12
12
---
13
13
14
-
Refs provide a way to access DOM nodes and React elements created by React by getting a _reference_ to the element in the render method.
14
+
Refs provide a way to access DOM nodes or React elements created in the render method.
15
15
16
16
In the typical React dataflow, [props](/docs/components-and-props.html) are the only way that parent components interact with their children. To modify a child, you re-render it with new props. However, there are a few cases where you need to imperatively modify a child outside of the typical dataflow. The child to be modified could be an instance of a React component, or it could be a DOM element. For both of these cases, React provides an escape hatch.
17
17
@@ -31,9 +31,9 @@ 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
-
>**Note:**
34
+
>Note
35
35
>
36
-
>The examples below have updated to use the `React.createRef()` API introduced in React 16.3.
36
+
>The examples below have been updated to use the React.createRef() API introduced in React 16.3. If you are using an earlier release of React, we recommend using [#callback-refs](callback refs) instead.
37
37
38
38
### Creating Refs
39
39
@@ -59,10 +59,10 @@ When a ref is passed to an element in `render`, a reference to the node becomes
59
59
constnode=this.myRef.value
60
60
```
61
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:
62
+
The value of the ref differs depending on the type of the node:
63
63
64
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`.
65
+
- When the `ref` attribute is used on a custom class component, the `ref` object receives the mounted instance of the component as its `value`.
66
66
-**You may not use the `ref` attribute on functional components** because they don't have instances.
67
67
68
68
The examples below demonstrate the differences.
@@ -195,7 +195,7 @@ In rare cases, you might want to have access to a child's DOM node from a parent
195
195
196
196
While you could [add a ref to the child component](#adding-a-ref-to-a-class-component), this is not an ideal solution, as you would only get a component instance rather than a DOM node. Additionally, this wouldn't work with functional components.
197
197
198
-
Instead, in such cases we recommend exposing a special prop on the child. The child would take a prop with an arbitrary name (e.g. `inputRef`) and attach it to the DOM node as a `ref` attribute. This lets the parent pass its ref to the child's DOM node through the component in the middle.
198
+
Instead, in such cases we recommend exposing a special prop on the child. This prop can be named anything other than ref (e.g. inputRef). The child component can then forward the prop to the DOM node as a ref attribute. This lets the parent pass its ref to the child's DOM node through the component in the middle.
199
199
200
200
This works both for classes and for functional components.
201
201
@@ -261,7 +261,7 @@ class Grandparent extends React.Component {
261
261
262
262
Here, the ref `this.inputElement` is first specified by `Grandparent`. It is passed to the `Parent` as a regular prop called `inputRef`, and the `Parent` passes it to the `CustomTextInput` as a prop too. Finally, the `CustomTextInput` reads the `inputRef` prop and attaches the passed ref as a `ref` attribute to the `<input>`. As a result, `this.inputElement.value` in `Grandparent` will be set to the DOM node corresponding to the `<input>` element in the `CustomTextInput`.
263
263
264
-
All things considered, we advise against exposing DOM nodes whenever possible, but this can be a useful escape hatch. Note that this approach requires you to add some code to the child component. If you have absolutely no control over the child component implementation, your last option is to use [`findDOMNode()`](/docs/react-dom.html#finddomnode), but it is discouraged.
264
+
When possible, we advise against exposing DOM nodes, but it can be a useful escape hatch. Note that this approach requires you to add some code to the child component. If you have absolutely no control over the child component implementation, your last option is to use [`findDOMNode()`](/docs/react-dom.html#finddomnode), but it is discouraged.
265
265
266
266
### Callback Refs
267
267
@@ -341,7 +341,11 @@ In the example above, `Parent` passes its ref callback as an `inputRef` prop to
341
341
342
342
### Legacy API: String Refs
343
343
344
-
If you worked with React before, you might be familiar with an older API where the `ref` attribute is a string, like `"textInput"`, and the DOM node is accessed as `this.refs.textInput`. We advise against it because string refs have [some issues](https://github.com/facebook/react/pull/8333#issuecomment-271648615), are considered legacy, and **are likely to be removed in one of the future releases**. If you're currently using `this.refs.textInput` to access refs, we recommend the callback pattern instead.
344
+
If you worked with React before, you might be familiar with an older API where the `ref` attribute is a string, like `"textInput"`, and the DOM node is accessed as `this.refs.textInput`. We advise against it because string refs have [some issues](https://github.com/facebook/react/pull/8333#issuecomment-271648615), are considered legacy, and **are likely to be removed in one of the future releases**.
345
+
346
+
> Note
347
+
>
348
+
> If you're currently using `this.refs.textInput` to access refs, we recommend the callback pattern instead.
0 commit comments