The library for web and native user interfaces.
Cleanup "More About Refs"
Paul O’Shannessy committed
Jul 16, 2013 at 23:30 UTC
f2b92d4c7bd7a71ec1118696c119bac22d4b836c
1 file changed
+14
-15
docs/docs/07.1-more-about-refs.md
+14
-15
@@ -39,9 +39,11 @@ Consider the case when you wish to tell an `<input />` element (that exists with
39
```
40
41
42
-Notice how, in this example, we want to "tell" the input something - something that it cannot infer from it's props over time. In this case we want to "tell" it that it should now become focused. However, there are some challenges. What is returned from render is not your actual composition of "child" components, it is merely a *description* of the children at a particular instance in time - a snapshot, if you will.
42
+Notice how, in this example, we want to "tell" the input something - something that it cannot infer from it's props over time. In this case we want to "tell" it that it should now become focused. However, there are some challenges. What is returned from `render()`` is not your actual composition of "child" components, it is merely a *description* of the children at a particular instance in time - a snapshot, if you will.
43
44
-NOTE: Remember, what you return from `render()` is not your *actual* rendered children instances. What you return from `render()` is merely a *description* of the children instances in your component's sub-hierarchy at a particular moment in time.
44
+> Note:
45
+>
46
+> Remember, what you return from `render()` is not your *actual* rendered children instances. What you return from `render()` is merely a *description* of the children instances in your component's sub-hierarchy at a particular moment in time.
47
48
49
This means that you should never "hold onto" something that you return from `render()` and then expect it to be anything meaningful.
@@ -64,19 +66,19 @@ In this counterexample, the `<input />` is merely a *description* of an `<input
66
67
So how do we talk to the *real* backing instance of the input?
68
67
-## The `ref` Attribute
69
+## The ref Attribute
70
71
React supports a very special property that you can attach to any component that is output from `render()`. This special property allows you to refer to the corresponding **backing instance** of anything returned from `render()`. It is always guaranteed to be the proper instance, at any point in time.
72
73
It's as simple as:
74
73
-1. Assign a `ref` attribute to anything returned from `render` such as:
75
+**1.** Assign a `ref` attribute to anything returned from `render` such as:
76
77
```html
78
<input ref="myInput" />
79
```
80
79
-2. In some other code (typically event handler code), access the **backing instance** via `this.refs` as in:
81
+**2.** In some other code (typically event handler code), access the **backing instance** via `this.refs` as in:
82
83
```javascript
84
this.refs.myInput
@@ -113,24 +115,21 @@ It's as simple as:
115
});
116
```
117
116
-In this example, our render function returns a description of an <input /> instance. But the true instance is accessed via `this.refs.theInput`. As long as a child component with `ref="theInput"` is returned from render, `this.refs.theInput` will access the the proper instance. This even works on higher level (non-DOM) components such as <Typeahead ref="myTypeahead" />.
118
+In this example, our render function returns a description of an `<input />` instance. But the true instance is accessed via `this.refs.theInput`. As long as a child component with `ref="theInput"` is returned from render, `this.refs.theInput` will access the the proper instance. This even works on higher level (non-DOM) components such as `<Typeahead ref="myTypeahead" />`.
119
120
121
## Summary
122
123
Refs are a great way to send a message to a particular child instance in a way that would be inconvenient to do via streaming Reactive `props` and `state`. They should, however, not be your go-to abstraction for flowing data through your application. By default, use the Reactive data flow and save `ref`s for use cases that are inherently non-reactive.
124
123
-**Benefits**:
125
+### Benefits:
126
125
-- You can define any public method on your component classes (such as a reset method on a Typeahead) and call those public methods through refs (such as this.refs.myTypeahead.reset()).
127
+- You can define any public method on your component classes (such as a reset method on a Typeahead) and call those public methods through refs (such as `this.refs.myTypeahead.reset()`).
128
+- Performing DOM measurements almost always requires reaching out to a "native" component such as `<input />` and accessing its underlying DOM node via `this.refs.myInput.getDOMNode()`. Refs are one of the only practical ways of doing this reliably.
129
+- Refs are automatically book-kept for you! If that child is destroyed, its ref is also destroyed for you. No worrying about memory here (unless you do something crazy to retain a reference yourself).
130
127
-- Performing DOM measurements almost always requires reaching out to a "native" component such as <input /> and accessing its underlying DOM node via this.refs.myInput.getDOMNode(). Refs are one of the only practical ways of doing this reliably.
128
-Refs are automatically book-kept for you! If that child is destroyed, its ref is also destroyed for you. No worrying about memory here (unless you do something crazy to retain a reference yourself).
129
-
130
-**Cautions**:
131
-
132
-- **Never** access refs inside of any component's render method - or while any component's render method is even running anywhere in the call stack.
131
+### Cautions:
132
133
+- *Never* access refs inside of any component's render method - or while any component's render method is even running anywhere in the call stack.
134
- If you want to preserve Google Closure Compiler Crushing resilience, make sure to never access as a property what was specified as a string. This means you must access using `this.refs['myRefString']` if your ref was defined as `ref="myRefString"`.
135
-
135
- If you have not programmed several apps with React, your first inclination is usually going to be to try 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. Placing the state there often eliminates any desire to use `ref`s to "make things happen" - instead, the data flow will usually accomplish your goal.