UNPKG

2.46 kBJavaScriptView Raw
1/**
2 * Copyright 2013-present, 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 */
10
11'use strict';
12
13var _prodInvariant = require('./reactProdInvariant');
14
15var ReactCurrentOwner = require('react/lib/ReactCurrentOwner');
16var ReactDOMComponentTree = require('./ReactDOMComponentTree');
17var ReactInstanceMap = require('./ReactInstanceMap');
18
19var getHostComponentFromComposite = require('./getHostComponentFromComposite');
20var invariant = require('fbjs/lib/invariant');
21var warning = require('fbjs/lib/warning');
22
23/**
24 * Returns the DOM node rendered by this element.
25 *
26 * See https://facebook.github.io/react/docs/top-level-api.html#reactdom.finddomnode
27 *
28 * @param {ReactComponent|DOMElement} componentOrElement
29 * @return {?DOMElement} The root node of this element.
30 */
31function findDOMNode(componentOrElement) {
32 if (process.env.NODE_ENV !== 'production') {
33 var owner = ReactCurrentOwner.current;
34 if (owner !== null) {
35 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;
36 owner._warnedAboutRefsInRender = true;
37 }
38 }
39 if (componentOrElement == null) {
40 return null;
41 }
42 if (componentOrElement.nodeType === 1) {
43 return componentOrElement;
44 }
45
46 var inst = ReactInstanceMap.get(componentOrElement);
47 if (inst) {
48 inst = getHostComponentFromComposite(inst);
49 return inst ? ReactDOMComponentTree.getNodeFromInstance(inst) : null;
50 }
51
52 if (typeof componentOrElement.render === 'function') {
53 !false ? process.env.NODE_ENV !== 'production' ? invariant(false, 'findDOMNode was called on an unmounted component.') : _prodInvariant('44') : void 0;
54 } else {
55 !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;
56 }
57}
58
59module.exports = findDOMNode;
\No newline at end of file