Created
January 25, 2015 02:56
-
-
Save trotzig/9b4848c45fa6beb15269 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Convenience method to resolve a component or a DOM node to a DOM node. If the | |
* passed in `node` is a React component, the root DOM node of the component is | |
* returned. If `node` is already a DOM node, that node is returned in itself. | |
* | |
* @param {Object} node A React component or a DOM node | |
* @return {?Object} a DOM node | |
*/ | |
var resolveDOMNode = (node) => { | |
if (!node) { | |
throw new Error(` | |
Can't resolve DOM node for an undefined node. The component may not be | |
initialized, or "Utils.findElement" returned undefined. | |
`); | |
} | |
if (node.getDOMNode) { | |
node = node.getDOMNode(); | |
} | |
// As of React 0.11.0, components can render as null, which means that they | |
// are still React components but `getDOMNode()` will return `null`. | |
if (node === null) { | |
return null; | |
} | |
if (!(node instanceof HTMLElement)) { | |
throw new Error(`node is not an HTMLElement, got ${node.constructor.name}: ${node.toString()}`); | |
} | |
return node; | |
}; | |
module.exports = resolveDOMNode; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment