UNPKG

2.34 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2013-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 */
8
9'use strict';
10
11var _prodInvariant = require('./reactProdInvariant');
12
13var ReactCurrentOwner = require('react/lib/ReactCurrentOwner');
14var ReactDOMComponentTree = require('./ReactDOMComponentTree');
15var ReactInstanceMap = require('./ReactInstanceMap');
16
17var getHostComponentFromComposite = require('./getHostComponentFromComposite');
18var invariant = require('fbjs/lib/invariant');
19var warning = require('fbjs/lib/warning');
20
21/**
22 * Returns the DOM node rendered by this element.
23 *
24 * See https://facebook.github.io/react/docs/top-level-api.html#reactdom.finddomnode
25 *
26 * @param {ReactComponent|DOMElement} componentOrElement
27 * @return {?DOMElement} The root node of this element.
28 */
29function findDOMNode(componentOrElement) {
30 if (process.env.NODE_ENV !== 'production') {
31 var owner = ReactCurrentOwner.current;
32 if (owner !== null) {
33 process.env.NODE_ENV !== 'production' ? warning(owner._warnedAboutRefsInRender, '%s is accessing findDOMNode inside its render(). ' + 'render() should be a pure function of props and state. It should ' + 'never access something that requires stale data from the previous ' + 'render, such as refs. Move this logic to componentDidMount and ' + 'componentDidUpdate instead.', owner.getName() || 'A component') : void 0;
34 owner._warnedAboutRefsInRender = true;
35 }
36 }
37 if (componentOrElement == null) {
38 return null;
39 }
40 if (componentOrElement.nodeType === 1) {
41 return componentOrElement;
42 }
43
44 var inst = ReactInstanceMap.get(componentOrElement);
45 if (inst) {
46 inst = getHostComponentFromComposite(inst);
47 return inst ? ReactDOMComponentTree.getNodeFromInstance(inst) : null;
48 }
49
50 if (typeof componentOrElement.render === 'function') {
51 !false ? process.env.NODE_ENV !== 'production' ? invariant(false, 'findDOMNode was called on an unmounted component.') : _prodInvariant('44') : void 0;
52 } else {
53 !false ? process.env.NODE_ENV !== 'production' ? invariant(false, 'Element appears to be neither ReactComponent nor DOMNode (keys: %s)', Object.keys(componentOrElement)) : _prodInvariant('45', Object.keys(componentOrElement)) : void 0;
54 }
55}
56
57module.exports = findDOMNode;
\No newline at end of file