Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Detect duplicated React #714

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion backend/installGlobalHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,69 @@ function installGlobalHook(window: Object) {
}
return 'production';
}
function detectRendererName(renderer) {
if (renderer.rendererPackageName) {
return renderer.rendererPackageName;
}
// Try to detect old versions without id injected
try {
var toString = Function.prototype.toString;
if (typeof renderer.version === 'string') {
// React DOM Fiber (16+)
return 'react-dom';
}
if (renderer.Mount && renderer.Mount._renderNewRootComponent) {
// React DOM Stack like renderer
var renderRootCode = toString.call(
renderer.Mount._renderNewRootComponent
);

// React DOM 15.*
if (
renderRootCode.indexOf('ensureScrollValueMonitoring') !== -1 &&
renderRootCode.indexOf('37') !== -1
) {
return 'react-dom';
}
// React DOM Stack 0.13.*/0.14.*
if (renderRootCode.indexOf('_registerComponent') !== -1) {
return 'react-dom';
}
// Something we're not aware of => not ReactDOM
return 'unknown';
}
} catch (err) {
// TODO: Mirrors error handling of detectReactBuildType()
}
return 'unknown';
}
function detectDuplicatedRenderers(renderers) {
// Detect if we have more than one ReactDOM instance
var renderersMap = {};

Object.keys(renderers).forEach(id => {
const name = detectRendererName(renderers[id]);

if (name !== 'unknown') {
renderersMap[name] = renderersMap[name] ? renderersMap[name] + 1 : 1;
}
});
return Object.keys(renderersMap).map(key => renderersMap[key]).some(rendererCount => rendererCount > 1);
}
const hook = ({
// Shared between Stack and Fiber:
_renderers: {},
helpers: {},
inject: function(renderer) {
var id = Math.random().toString(16).slice(2);
hook._renderers[id] = renderer;
var reactBuildType = detectReactBuildType(renderer);
var isReactDuplicated = detectDuplicatedRenderers(hook._renderers);

// Currently we overwrite buildType with each injected renderer's type
var reactBuildType = isReactDuplicated
? 'duplicated'
: detectReactBuildType(renderer);

hook.emit('renderer', {id, renderer, reactBuildType});
return id;
},
Expand Down
Binary file added shells/webextension/icons/128-duplicated.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added shells/webextension/icons/16-duplicated.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added shells/webextension/icons/32-duplicated.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added shells/webextension/icons/48-duplicated.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions shells/webextension/icons/duplicated.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions shells/webextension/popups/duplicated.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script src="shared.js"></script>
<style>
html, body {
font-size: 14px;
min-width: 430px;
min-height: 101px;
}
</style>
<p>
<b>This page is using multiple copies of React. &#x1f6a7;</b>
</p>
<p>
Note that using several copies of React may cause problems.
<br />
<br />
You can find the troubleshooting instructions in <a href="https://fb.me/react-refs-must-have-owner#multiple-copies-of-react">React Blog</a>.
</p>
<hr />
<p>
Open the developer tools, and the React tab will appear to the right.
</p>