Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* 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