Skip to content

Commit 030fc75

Browse files
committed
Updated docs examples/recommendations to use findDOMNode instead of getDOMNode
1 parent fa89611 commit 030fc75

File tree

7 files changed

+34
-28
lines changed

7 files changed

+34
-28
lines changed

docs/docs/08-working-with-the-browser.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ Additionally, React implements a full synthetic event system such that all event
1818
Most of the time you should stay within React's "faked browser" world since it's more performant and easier to reason about. However, sometimes you simply need to access the underlying API, perhaps to work with a third-party library like a jQuery plugin. React provides escape hatches for you to use the underlying DOM API directly.
1919

2020

21-
## Refs and getDOMNode()
21+
## Refs and findDOMNode()
2222

23-
To interact with the browser, you'll need a reference to a DOM node. Every mounted React component has a `getDOMNode()` function which you can call to get a reference to it.
23+
To interact with the browser, you'll need a reference to a DOM node. React has a `React.findDOMNode(component)` function which you can call to get a reference to the component's DOM node.
2424

2525
> Note:
2626
>
27-
> `getDOMNode()` only works on mounted components (that is, components that have been placed in the DOM). If you try to call this on a component that has not been mounted yet (like calling `getDOMNode()` in `render()` on a component that has yet to be created) an exception will be thrown.
27+
> `findDOMNode()` only works on mounted components (that is, components that have been placed in the DOM). If you try to call this on a component that has not been mounted yet (like calling `findDOMNode()` in `render()` on a component that has yet to be created) an exception will be thrown.
2828
2929
In order to get a reference to a React component, you can either use `this` to get the current React component, or you can use refs to refer to a component you own. They work like this:
3030

3131
```javascript
3232
var MyComponent = React.createClass({
3333
handleClick: function() {
3434
// Explicitly focus the text input using the raw DOM API.
35-
this.refs.myTextInput.getDOMNode().focus();
35+
React.findDOMNode(this.refs.myTextInput).focus();
3636
},
3737
render: function() {
3838
// The ref attribute adds a reference to the component to
@@ -97,7 +97,7 @@ React provides lifecycle methods that you can specify to hook into this process.
9797

9898
_Mounted_ composite components also support the following methods:
9999

100-
* `getDOMNode(): DOMElement` can be invoked on any mounted component in order to obtain a reference to its rendered DOM node.
100+
* `findDOMNode(): DOMElement` can be invoked on any mounted component in order to obtain a reference to its rendered DOM node.
101101
* `forceUpdate()` can be invoked on any mounted component when you know that some deeper aspect of the component's state has changed without using `this.setState()`.
102102

103103

docs/docs/08.1-more-about-refs.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ It's as simple as:
8383
this.refs.myInput
8484
```
8585

86-
You can access the component's DOM node directly by calling `this.refs.myInput.getDOMNode()`.
86+
You can access the component's DOM node directly by calling `React.findDOMNode(this.refs.myInput)`.
8787

8888
## Completing the Example
8989

@@ -99,7 +99,7 @@ It's as simple as:
9999
// Clear the input
100100
this.setState({userInput: ''}, function() {
101101
// This code executes after the component is re-rendered
102-
this.refs.theInput.getDOMNode().focus(); // Boom! Focused!
102+
React.findDOMNode(this.refs.theInput).focus(); // Boom! Focused!
103103
});
104104
},
105105
render: function() {
@@ -129,7 +129,7 @@ Refs are a great way to send a message to a particular child instance in a way t
129129
### Benefits:
130130

131131
- 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()`).
132-
- 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.
132+
- Performing DOM measurements almost always requires reaching out to a "native" component such as `<input />` and accessing its underlying DOM node via `React.findDOMNode(this.refs.myInput)`. Refs are one of the only practical ways of doing this reliably.
133133
- 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).
134134

135135
### Cautions:

docs/docs/10.4-test-utils.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Simulate an event dispatch on a DOM node with optional `eventData` event data. *
1919
Example usage:
2020

2121
```javascript
22-
var node = this.refs.input.getDOMNode();
22+
var node = React.findDOMNode(this.refs.input);
2323
React.addons.TestUtils.Simulate.click(node);
2424
React.addons.TestUtils.Simulate.change(node, {target: {value: 'Hello, world'}});
2525
React.addons.TestUtils.Simulate.keyDown(node, {key: "Enter"});

docs/docs/ref-01-top-level-api.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ boolean isValidElement(* object)
110110
Verifies the object is a ReactElement.
111111

112112

113+
### React.findDOMNode
114+
115+
```javascript
116+
DOMElement findDOMNode(ReactComponent component)
117+
```
118+
If this component has been mounted into the DOM, this returns the corresponding native browser DOM element. This method is useful for reading values out of the DOM, such as form field values and performing DOM measurements. When `render` returns `null` or `false`, `this.getDOMNode()` returns `null`.
119+
120+
113121
### React.DOM
114122

115123
`React.DOM` provides convenience wrappers around `React.createElement` for DOM components. These should only be used when not using JSX. For example, `React.DOM.div(null, 'Hello World!')`

docs/docs/ref-02-component-api.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ DOMElement getDOMNode()
6060

6161
If this component has been mounted into the DOM, this returns the corresponding native browser DOM element. This method is useful for reading values out of the DOM, such as form field values and performing DOM measurements. When `render` returns `null` or `false`, `this.getDOMNode()` returns `null`.
6262

63+
> Note:
64+
>
65+
> getDOMNode is deprecated and has been replaced with [React.findDOMNode()](/react/docs/top-level-api.html#react.finddomnode).
66+
6367

6468
### isMounted
6569

docs/docs/ref-03-component-specs.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ The `render()` method is required.
2121

2222
When called, it should examine `this.props` and `this.state` and return a single child component. This child component can be either a virtual representation of a native DOM component (such as `<div />` or `React.DOM.div()`) or another composite component that you've defined yourself.
2323

24-
You can also return `null` or `false` to indicate that you don't want anything rendered. Behind the scenes, React renders a `<noscript>` tag to work with our current diffing algorithm. When returning `null` or `false`, `this.getDOMNode()` will return `null`.
24+
You can also return `null` or `false` to indicate that you don't want anything rendered. Behind the scenes, React renders a `<noscript>` tag to work with our current diffing algorithm. When returning `null` or `false`, `React.findDOMNode(this)` will return `null`.
2525

2626
The `render()` function should be *pure*, meaning that it does not modify component state, it returns the same result each time it's invoked, and it does not read from or write to the DOM or otherwise interact with the browser (e.g., by using `setTimeout`). If you need to interact with the browser, perform your work in `componentDidMount()` or the other lifecycle methods instead. Keeping `render()` pure makes server rendering more practical and makes components easier to think about.
2727

@@ -118,14 +118,10 @@ Invoked once, both on the client and server, immediately before the initial rend
118118
componentDidMount()
119119
```
120120

121-
Invoked once, only on the client (not on the server), immediately after the initial rendering occurs. At this point in the lifecycle, the component has a DOM representation which you can access via `this.getDOMNode()`.
121+
Invoked once, only on the client (not on the server), immediately after the initial rendering occurs. At this point in the lifecycle, the component has a DOM representation which you can access via `React.findDOMNode(this)`.
122122

123123
If you want to integrate with other JavaScript frameworks, set timers using `setTimeout` or `setInterval`, or send AJAX requests, perform those operations in this method.
124124

125-
> Note:
126-
>
127-
> Prior to v0.9, the DOM node was passed in as the last argument. If you were using this, you can still access the DOM node by calling `this.getDOMNode()`.
128-
129125

130126
### Updating: componentWillReceiveProps
131127

@@ -199,10 +195,6 @@ Invoked immediately after the component's updates are flushed to the DOM. This m
199195

200196
Use this as an opportunity to operate on the DOM when the component has been updated.
201197

202-
> Note:
203-
>
204-
> Prior to v0.9, the DOM node was passed in as the last argument. If you were using this, you can still access the DOM node by calling `this.getDOMNode()`.
205-
206198

207199
### Unmounting: componentWillUnmount
208200

docs/docs/tutorial.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -487,14 +487,15 @@ Let's make the form interactive. When the user submits the form, we should clear
487487
var CommentForm = React.createClass({
488488
handleSubmit: function(e) {
489489
e.preventDefault();
490-
var author = this.refs.author.getDOMNode().value.trim();
491-
var text = this.refs.text.getDOMNode().value.trim();
490+
var author = React.findDOMNode(this.refs.author).value.trim();
491+
var text = React.findDOMNode(this.refs.text).value.trim();
492492
if (!text || !author) {
493493
return;
494494
}
495495
// TODO: send request to the server
496-
this.refs.author.getDOMNode().value = '';
497-
this.refs.text.getDOMNode().value = '';
496+
React.findDOMNode(this.refs.author).value = '';
497+
React.findDOMNode(this.refs.text).value = '';
498+
return;
498499
},
499500
render: function() {
500501
return (
@@ -516,7 +517,7 @@ Call `preventDefault()` on the event to prevent the browser's default action of
516517

517518
##### Refs
518519

519-
We use the `ref` attribute to assign a name to a child component and `this.refs` to reference the component. We can call `getDOMNode()` on a component to get the native browser DOM element.
520+
We use the `ref` attribute to assign a name to a child component and `this.refs` to reference the component. We can call `React.findDOMNode(component)` on a component to get the native browser DOM element.
520521

521522
##### Callbacks as props
522523

@@ -568,14 +569,15 @@ Let's call the callback from the `CommentForm` when the user submits the form:
568569
var CommentForm = React.createClass({
569570
handleSubmit: function(e) {
570571
e.preventDefault();
571-
var author = this.refs.author.getDOMNode().value.trim();
572-
var text = this.refs.text.getDOMNode().value.trim();
572+
var author = React.findDOMNode(this.refs.author).value.trim();
573+
var text = React.findDOMNode(this.refs.text).value.trim();
573574
if (!text || !author) {
574575
return;
575576
}
576577
this.props.onCommentSubmit({author: author, text: text});
577-
this.refs.author.getDOMNode().value = '';
578-
this.refs.text.getDOMNode().value = '';
578+
React.findDOMNode(this.refs.author).value = '';
579+
React.findDOMNode(this.refs.text).value = '';
580+
return;
579581
},
580582
render: function() {
581583
return (

0 commit comments

Comments
 (0)