Skip to content

Commit e072534

Browse files
committed
Merge pull request #2646 from jsfb/getDOMNode-becomes-findDOMNode
Added findDOMNode, as we move toward deprecating getDOMNode
2 parents f7166d0 + b46a6ce commit e072534

File tree

4 files changed

+105
-8
lines changed

4 files changed

+105
-8
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright 2013-2014, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @emails react-core
10+
*/
11+
12+
"use strict";
13+
14+
var React = require('React');
15+
var ReactTestUtils = require('ReactTestUtils');
16+
17+
describe('findDOMNode', function() {
18+
it('findDOMNode should return null if passed null', function() {
19+
expect(React.findDOMNode(null)).toBe(null);
20+
});
21+
22+
it('findDOMNode should find dom element', function() {
23+
var MyNode = React.createClass({
24+
render: function() {
25+
return <div><span>Noise</span></div>;
26+
}
27+
});
28+
29+
var myNode = ReactTestUtils.renderIntoDocument(<MyNode />);
30+
var myDiv = React.findDOMNode(myNode);
31+
var mySameDiv = React.findDOMNode(myDiv);
32+
expect(myDiv.tagName).toBe('DIV');
33+
expect(mySameDiv).toBe(myDiv);
34+
});
35+
36+
it('findDOMNode should reject random objects', function() {
37+
expect(function() {React.findDOMNode({foo: 'bar'});})
38+
.toThrow('Invariant Violation: Element appears to be neither ' +
39+
'ReactComponent nor DOMNode (keys: foo)'
40+
);
41+
});
42+
43+
it('findDOMNode should reject unmounted objects with render func', function() {
44+
expect(function() {React.findDOMNode({render: function(){}});})
45+
.toThrow('Invariant Violation: Component contains `render` ' +
46+
'method but is not mounted in the DOM'
47+
);
48+
});
49+
50+
});
51+

src/browser/findDOMNode.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright 2013-2014, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @providesModule findDOMNode
10+
* @typechecks static-only
11+
*/
12+
13+
"use strict";
14+
15+
var ReactComponent = require('ReactComponent');
16+
var ReactInstanceMap = require('ReactInstanceMap');
17+
var ReactMount = require('ReactMount');
18+
19+
var invariant = require('invariant');
20+
var isNode = require('isNode');
21+
22+
/**
23+
* Returns the DOM node rendered by this element.
24+
*
25+
* @param {ReactComponent|DOMElement} element
26+
* @return {DOMElement} The root node of this element.
27+
*/
28+
function findDOMNode(componentOrElement) {
29+
if (componentOrElement == null) {
30+
return null;
31+
}
32+
if (isNode(componentOrElement)) {
33+
return componentOrElement;
34+
}
35+
if (ReactInstanceMap.has(componentOrElement)) {
36+
return ReactMount.getNodeFromInstance(componentOrElement);
37+
}
38+
invariant(
39+
!(componentOrElement.render != null && typeof(componentOrElement.render) === 'function'),
40+
'Component contains `render` method but is not mounted in the DOM',
41+
Object.keys(componentOrElement)
42+
);
43+
invariant(
44+
false,
45+
'Element appears to be neither ReactComponent nor DOMNode (keys: %s)',
46+
Object.keys(componentOrElement)
47+
);
48+
}
49+
50+
module.exports = findDOMNode;

src/browser/ui/React.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var ReactRef = require('ReactRef');
3535
var ReactServerRendering = require('ReactServerRendering');
3636

3737
var assign = require('Object.assign');
38+
var findDOMNode = require('findDOMNode');
3839
var onlyChild = require('onlyChild');
3940

4041
ReactDefaultInjection.inject();
@@ -69,6 +70,7 @@ var React = {
6970
},
7071
constructAndRenderComponent: ReactMount.constructAndRenderComponent,
7172
constructAndRenderComponentByID: ReactMount.constructAndRenderComponentByID,
73+
findDOMNode: findDOMNode,
7274
render: render,
7375
renderToString: ReactServerRendering.renderToString,
7476
renderToStaticMarkup: ReactServerRendering.renderToStaticMarkup,

src/browser/ui/ReactBrowserComponentMixin.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111

1212
"use strict";
1313

14-
var ReactMount = require('ReactMount');
15-
16-
var invariant = require('invariant');
14+
var findDOMNode = require('findDOMNode');
1715

1816
var ReactBrowserComponentMixin = {
1917
/**
@@ -24,11 +22,7 @@ var ReactBrowserComponentMixin = {
2422
* @protected
2523
*/
2624
getDOMNode: function() {
27-
invariant(
28-
this.isMounted(),
29-
'getDOMNode(): A component must be mounted to have a DOM node.'
30-
);
31-
return ReactMount.getNodeFromInstance(this);
25+
return findDOMNode(this);
3226
}
3327
};
3428

0 commit comments

Comments
 (0)