mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
2490289c4a
This is an anti-pattern that can be easily avoided by putting the logic in componentDidMount and componentDidUpdate instead. It creates a dependency on stale data inside render without enforcing a two-pass render.
69 lines
2.1 KiB
JavaScript
69 lines
2.1 KiB
JavaScript
/**
|
|
* Copyright 2013-2015, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*
|
|
* @providesModule findDOMNode
|
|
* @typechecks static-only
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
var ReactCurrentOwner = require('ReactCurrentOwner');
|
|
var ReactInstanceMap = require('ReactInstanceMap');
|
|
var ReactMount = require('ReactMount');
|
|
|
|
var invariant = require('invariant');
|
|
var isNode = require('isNode');
|
|
var warning = require('warning');
|
|
|
|
/**
|
|
* Returns the DOM node rendered by this element.
|
|
*
|
|
* @param {ReactComponent|DOMElement} componentOrElement
|
|
* @return {DOMElement} The root node of this element.
|
|
*/
|
|
function findDOMNode(componentOrElement) {
|
|
if (__DEV__) {
|
|
var owner = ReactCurrentOwner.current;
|
|
if (owner !== null) {
|
|
warning(
|
|
owner._warnedAboutRefsInRender,
|
|
'%s is accessing getDOMNode or 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'
|
|
);
|
|
owner._warnedAboutRefsInRender = true;
|
|
}
|
|
}
|
|
if (componentOrElement == null) {
|
|
return null;
|
|
}
|
|
if (isNode(componentOrElement)) {
|
|
return componentOrElement;
|
|
}
|
|
if (ReactInstanceMap.has(componentOrElement)) {
|
|
return ReactMount.getNodeFromInstance(componentOrElement);
|
|
}
|
|
invariant(
|
|
componentOrElement.render == null ||
|
|
typeof componentOrElement.render !== 'function',
|
|
'Component (with keys: %s) contains `render` method ' +
|
|
'but is not mounted in the DOM',
|
|
Object.keys(componentOrElement)
|
|
);
|
|
invariant(
|
|
false,
|
|
'Element appears to be neither ReactComponent nor DOMNode (keys: %s)',
|
|
Object.keys(componentOrElement)
|
|
);
|
|
}
|
|
|
|
module.exports = findDOMNode;
|