Improve findComponentRoot Error Message

Instead of simply logging the React ID of the `ancestorNode` when `findComponentRoot` fails, use a `try ... finally` to `console.error` the `ancestorNode`. This allows modern web inspectors to actually log a reference to the node (which may not have a React ID).

This means when people run into the problem, they will not have to execute:

  require('ReactID').getNode(<copy+paste>);

NOTE: Admittedly, this will not log anything in IE8. That's fine, since IE8 has shitty console logging anyway.
This commit is contained in:
CommitSyncScript
2013-06-28 16:33:45 -07:00
committed by Paul O’Shannessy
parent bd150ec658
commit 738de8cfa8
2 changed files with 10 additions and 8 deletions

View File

@@ -296,12 +296,15 @@ var ReactInstanceHandles = {
}
child = child.nextSibling;
}
global.console && console.error && console.error(
'Error while invoking `findComponentRoot` with the following ' +
'ancestor node:',
ancestorNode
);
invariant(
false,
'findComponentRoot: Unable to find element by React ID, `%s`. This ' +
'indicates that someone (or the browser) has mutated the DOM tree in ' +
'an unexpected way. Try inspecting the child nodes of the element with ' +
'React ID, `%s`.',
'findComponentRoot(..., %s): Unable to find element. This probably ' +
'means the DOM was unexpectedly mutated (e.g. by the browser).',
id,
ReactID.getID(ancestorNode)
);

View File

@@ -139,10 +139,9 @@ describe('ReactInstanceHandles', function() {
ReactID.getID(childNodeB)
);
}).toThrow(
'Invariant Violation: findComponentRoot: Unable to find element by ' +
'React ID, `.react[0].1:0`. This indicates that someone (or the ' +
'browser) has mutated the DOM tree in an unexpected way. Try ' +
'inspecting the child nodes of the element with React ID, `.react[0]`.'
'Invariant Violation: findComponentRoot(..., .react[0].1:0): Unable ' +
'to find element. This probably means the DOM was unexpectedly ' +
'mutated (e.g. by the browser).'
);
});
});