Skip to content

Instantly share code, notes, and snippets.

@trotzig
Created January 25, 2015 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trotzig/9b4848c45fa6beb15269 to your computer and use it in GitHub Desktop.
Save trotzig/9b4848c45fa6beb15269 to your computer and use it in GitHub Desktop.
/**
* 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