Added findDOMNode, as we move toward deprecating getDOMNode
Jim committed
Dec 2, 2014 at 16:44 UTC
b46a6ce4bb8d6087ed424764f41fe4b8e248b3b4
4 files changed
+105
-8
src/browser/__tests__/findDOMNode.-test.js
new
+51
@@ -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
new
+50
@@ -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
+2
@@ -35,6 +35,7 @@ var ReactRef = require('ReactRef');
35
var ReactServerRendering = require('ReactServerRendering');
36
37
var assign = require('Object.assign');
38
+var findDOMNode = require('findDOMNode');
39
var onlyChild = require('onlyChild');
40
41
ReactDefaultInjection.inject();
@@ -69,6 +70,7 @@ var React = {
70
},
71
constructAndRenderComponent: ReactMount.constructAndRenderComponent,
72
constructAndRenderComponentByID: ReactMount.constructAndRenderComponentByID,
73
+ findDOMNode: findDOMNode,
74
render: render,
75
renderToString: ReactServerRendering.renderToString,
76
renderToStaticMarkup: ReactServerRendering.renderToStaticMarkup,
src/browser/ui/ReactBrowserComponentMixin.js
+2
-8
@@ -11,9 +11,7 @@
11
12
"use strict";
13
14
-var ReactMount = require('ReactMount');
15
-
16
-var invariant = require('invariant');
14
+var findDOMNode = require('findDOMNode');
15
16
var ReactBrowserComponentMixin = {
17
/**
@@ -24,11 +22,7 @@ var ReactBrowserComponentMixin = {
22
* @protected
23
*/
24
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);
26
}
27
};
28