Skip to content

Commit 738de8c

Browse files
jeffmozpao
authored andcommitted
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.
1 parent bd150ec commit 738de8c

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/core/ReactInstanceHandles.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,15 @@ var ReactInstanceHandles = {
296296
}
297297
child = child.nextSibling;
298298
}
299+
global.console && console.error && console.error(
300+
'Error while invoking `findComponentRoot` with the following ' +
301+
'ancestor node:',
302+
ancestorNode
303+
);
299304
invariant(
300305
false,
301-
'findComponentRoot: Unable to find element by React ID, `%s`. This ' +
302-
'indicates that someone (or the browser) has mutated the DOM tree in ' +
303-
'an unexpected way. Try inspecting the child nodes of the element with ' +
304-
'React ID, `%s`.',
306+
'findComponentRoot(..., %s): Unable to find element. This probably ' +
307+
'means the DOM was unexpectedly mutated (e.g. by the browser).',
305308
id,
306309
ReactID.getID(ancestorNode)
307310
);

src/core/__tests__/ReactInstanceHandles-test.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,9 @@ describe('ReactInstanceHandles', function() {
139139
ReactID.getID(childNodeB)
140140
);
141141
}).toThrow(
142-
'Invariant Violation: findComponentRoot: Unable to find element by ' +
143-
'React ID, `.react[0].1:0`. This indicates that someone (or the ' +
144-
'browser) has mutated the DOM tree in an unexpected way. Try ' +
145-
'inspecting the child nodes of the element with React ID, `.react[0]`.'
142+
'Invariant Violation: findComponentRoot(..., .react[0].1:0): Unable ' +
143+
'to find element. This probably means the DOM was unexpectedly ' +
144+
'mutated (e.g. by the browser).'
146145
);
147146
});
148147
});

0 commit comments

Comments
 (0)