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: docs/docs/05-reusable-components.md
+30Lines changed: 30 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -232,3 +232,33 @@ Methods follow the same semantics as regular ES6 classes, meaning that they don'
232
232
### No Mixins
233
233
234
234
Unfortunately ES6 launched without any mixin support. Therefore, there is no support for mixins when you use React with ES6 classes. Instead, we're working on making it easier to support such use cases without resorting to mixins.
235
+
236
+
237
+
238
+
## Stateless Functions
239
+
240
+
You may also define your React classes as a plain JavaScript function. For example using the stateless function syntax:
This simplified component API is intended for components that are pure functions of their props. These components must not retain internal state, do not have backing instances, and do not have the component lifecycle methods. They are pure functional transforms of their input, with zero boilerplate.
258
+
259
+
> NOTE:
260
+
>
261
+
> Because stateless functions don't have a backing instance, you can't attach a ref to a stateless function component. Normally this isn't an issue, since stateless functions do not provide an imperative API, there really isn't much you could do with an instance anyway. However, if a user wants to find the DOM node of a stateless function component, they must wrap the component in a stateful component (eg. ES6 class component) and attach the ref to the stateful wrapper component.
262
+
263
+
In an ideal world, most of your components would be stateless functions because these stateless components can follow a faster code path within the React core. This is the recommended pattern, when possible.
Copy file name to clipboardExpand all lines: docs/docs/08.1-more-about-refs.md
+89-60Lines changed: 89 additions & 60 deletions
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,97 @@
1
1
---
2
2
id: more-about-refs
3
-
title: More About Refs
3
+
title: References to Components
4
4
permalink: more-about-refs.html
5
5
prev: working-with-the-browser.html
6
6
next: tooling-integration.html
7
7
---
8
-
After returning the structure of your UI from the render method, you may find yourself wanting to "reach out" and invoke methods on component instances returned from `render()`. Often, doing something like this isn't necessary for making data flow through your application, because the Reactive data flow always ensures that the most recent `props` are sent to each child that is output from `render()`. However, there are a few cases where it still might be necessary or beneficial.
8
+
After building your component, you may find yourself wanting to "reach out" and invoke methods on component instances returned from `render()`. In most cases, this is ideally unnecessary (and should be avoided) because the reactive data flow always ensures that the most recent props are sent to each child that is output from render(). However, there are a few cases where it still might be necessary or beneficial, so React provides an escape hatch known as `refs`. These `refs` (references) are especially useful when you need to find the DOM markup rendered by a component (for instance, to position it absolutely), when using React components in a larger non-React application, or when transitioning your code to React.
9
9
10
-
Consider the case, when you wish to tell an `<input />` element (that exists within your instances sub-hierarchy) to focus after you update its value to be the empty string, `''`.
10
+
Consider the case, when you wish to tell an `<input />` element (that exists within your instances sub-hierarchy) to focus after you update its value to be the empty string, `''`. That is to say, we want to implement an imperative function that will reset your application's input field to the empty state. This is a reasonable use case for refs, and one which we will explore in a moment, but first we'll look at the various options for getting a reference.
11
+
12
+
## The ref returned from React.render
13
+
14
+
No to be confused with the `render()` function that you define on your component (and which returns a virtual DOM element), [React.render()](/react/docs/top-level-api.html#react.render) will return a reference to your component's **backing instance** (or null for [stateless components](/react/docs/reusable-components.html#stateless-functions)).
15
+
16
+
17
+
```js
18
+
var myComponent =React.render(<MyComponent />, myContainer);
19
+
```
20
+
21
+
Keep in mind, however, that the JSX doesn't return a component instance! It's just a **ReactElement**: a lightweight representation that tells React what the mounted component should look like.
22
+
23
+
```js
24
+
var myComponentElement =<MyComponent />; // This is just a ReactElement.
25
+
26
+
// Some code here...
27
+
28
+
var myComponentInstance =React.render(myComponentElement, myContainer);
29
+
myComponentInstance.doSomething();
30
+
```
31
+
32
+
> Note:
33
+
>
34
+
> This should only ever be used at the top level. Inside components, let your `props` and `state` handle communication with child components, or use one of the other methods of getting a ref (string attribute or callbacks).
35
+
36
+
37
+
## The ref Callback Attribute
38
+
39
+
React supports a very special attribute that you can attach to any component. The `ref` attribute can be a callback function, and this callback will be executed immediately after the component is mounted. The referenced component will be passed in as a parameter, and the callback function may use the component immediately, or save the reference for future use (or both).
40
+
41
+
It's as simple as adding a `ref` attribute to anything returned from `render` by using an ES6 arrow function:
42
+
43
+
```html
44
+
render: function() {
45
+
return <TextInputref={(c) => this._input = c} />;
46
+
},
47
+
componentDidMount: function() {
48
+
this._input.focus();
49
+
},
50
+
```
51
+
52
+
or
53
+
54
+
```html
55
+
render: function() {
56
+
return (
57
+
<TextInput
58
+
ref={function(input){
59
+
if(input!= null){
60
+
input.focus();
61
+
}
62
+
}} />
63
+
);
64
+
},
65
+
```
66
+
67
+
Note that when the referenced component is unmounted and whenever the ref changes, the old ref will be called with `null` as an argument. This prevents memory leaks in the case that the instance is stored, as in the first example. Note that when writing refs with inline function expressions as in the examples here, React sees a different function object each time so on every update, ref will be called with `null` immediately before it's called with the component instance.
68
+
69
+
You can access the component's DOM node directly by calling `React.findDOMNode(argumentToYourCallback)`.
70
+
71
+
72
+
## The ref String Attribute
73
+
74
+
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.
75
+
76
+
1. Assign a `ref` attribute to anything returned from `render` such as:
77
+
78
+
```html
79
+
<inputref="myInput" />
80
+
```
81
+
82
+
2. In some other code (typically event handler code), access the **backing instance** via `this.refs` as in:
83
+
84
+
```javascript
85
+
this.refs.myInput
86
+
```
87
+
88
+
You can access the component's DOM node directly by calling `React.findDOMNode(this.refs.myInput)`.
89
+
90
+
91
+
## A Complete Example
92
+
93
+
94
+
Consider the case, when you wish to tell an `<input />` element (that exists within your instances sub-hierarchy) to focus after you update its value to be the empty string, `''`. That is to say, we want to implement an imperative function that will reset your application's input field to the empty state.
11
95
12
96
```javascript
13
97
var App = React.createClass({
@@ -19,7 +103,7 @@ Consider the case, when you wish to tell an `<input />` element (that exists wit
19
103
},
20
104
clearAndFocusInput: function() {
21
105
this.setState({userInput: ''}); // Clear the input
22
-
// We wish to focus the <input /> now!
106
+
// TODO: We wish to focus the <input /> now!
23
107
},
24
108
render: function() {
25
109
return (
@@ -44,7 +128,6 @@ Notice how, in this example, we want to "tell" the input something - something t
44
128
>
45
129
> 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.
46
130
47
-
48
131
This means that you should never "hold onto" something that you return from `render()` and then expect it to be anything meaningful.
49
132
50
133
```javascript
@@ -65,61 +148,6 @@ In this example, the `<input />` is merely a *description* of an `<input />`. Th
65
148
66
149
So how do we talk to the *real* backing instance of the input?
67
150
68
-
## The ref String Attribute
69
-
70
-
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.
71
-
72
-
It's as simple as:
73
-
74
-
1. Assign a `ref` attribute to anything returned from `render` such as:
75
-
76
-
```html
77
-
<inputref="myInput" />
78
-
```
79
-
80
-
2. In some other code (typically event handler code), access the **backing instance** via `this.refs` as in:
81
-
82
-
```javascript
83
-
this.refs.myInput
84
-
```
85
-
86
-
You can access the component's DOM node directly by calling `React.findDOMNode(this.refs.myInput)`.
87
-
88
-
89
-
## The ref Callback Attribute
90
-
91
-
The `ref` attribute can be a callback function instead of a name. This callback will be executed immediately after the component is mounted. The referenced component will be passed in as a parameter, and the callback function may use the component immediately, or save the reference for future use (or both).
92
-
93
-
It's as simple as adding a `ref` attribute to anything returned from `render` by using an ES6 arrow function:
94
-
95
-
```html
96
-
render: function() {
97
-
return <TextInputref={(c) => this._input = c} />;
98
-
},
99
-
componentDidMount: function() {
100
-
this._input.focus();
101
-
},
102
-
```
103
-
104
-
or
105
-
106
-
```html
107
-
render: function() {
108
-
return (
109
-
<TextInput
110
-
ref={function(input){
111
-
if(input!= null){
112
-
input.focus();
113
-
}
114
-
}} />
115
-
);
116
-
},
117
-
```
118
-
119
-
Note that when the referenced component is unmounted and whenever the ref changes, the old ref will be called with `null` as an argument. This prevents memory leaks in the case that the instance is stored, as in the first example. Note that when writing refs with inline function expressions as in the examples here, React sees a different function object each time so on every update, ref will be called with `null` immediately before it's called with the component instance.
120
-
121
-
122
-
## Completing the Example
123
151
124
152
```javascript
125
153
var App =React.createClass({
@@ -171,3 +199,4 @@ Refs are a great way to send a message to a particular child instance in a way t
171
199
-*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.
172
200
- 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"`.
173
201
- 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.
202
+
- Refs may not be attached to a [stateless function](/react/docs/reusable-components.html#stateless-functions), because the component does not have a backing instance. You can always wrap a stateless component in a standard composite component and attach a ref to the composite component.
Copy file name to clipboardExpand all lines: docs/docs/ref-01-top-level-api.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -81,7 +81,7 @@ ReactComponent render(
81
81
)
82
82
```
83
83
84
-
Render a ReactElement into the DOM in the supplied `container` and return a reference to the component.
84
+
Render a ReactElement into the DOM in the supplied `container` and return a [reference](/react/docs/more-about-refs.html) to the component (or returns null for [stateless components](/react/docs/reusable-components.html#stateless-functions)).
85
85
86
86
If the ReactElement was previously rendered into `container`, this will perform an update on it and only mutate the DOM as necessary to reflect the latest React component.
Copy file name to clipboardExpand all lines: docs/tips/15-expose-component-functions.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ title: Expose Component Functions
4
4
layout: tips
5
5
permalink: expose-component-functions.html
6
6
prev: communicate-between-components.html
7
-
next: references-to-components.html
7
+
next: children-undefined.html
8
8
---
9
9
10
10
There's another (uncommon) way of [communicating between components](/react/tips/communicate-between-components.html): simply expose a method on the child component for the parent to call.
If you're using React components in a larger non-React application or transitioning your code to React, you may need to keep references to components. `React.render` returns a reference to the mounted component:
11
-
12
-
```js
13
-
var myComponent =React.render(<MyComponent />, myContainer);
14
-
```
15
-
16
-
Keep in mind, however, that the JSX doesn't return a component instance! It's just a **ReactElement**: a lightweight representation that tells React what the mounted component should look like.
17
-
18
-
```js
19
-
var myComponentElement =<MyComponent />; // This is just a ReactElement.
20
-
21
-
// Some code here...
22
-
23
-
var myComponentInstance =React.render(myComponentElement, myContainer);
24
-
```
25
-
26
-
> Note:
27
-
>
28
-
> This should only ever be used at the top level. Inside components, let your `props` and `state` handle communication with child components, and only reference components via [refs](/react/docs/more-about-refs.html).
10
+
This page has moved to: [refs](/react/docs/more-about-refs.html).
0 commit comments