Making Browserify bundle play nice with ReactDevTools

≡放荡痞女 提交于 2019-12-07 04:48:28

It seems enough people have stumbled upon this matter, and the official guide does not mention this particular symptom. After gathering enough evidence, it is best that an answer is posted here, even if there might be other causes to this <Top Level></Top Level> issue.

The React DevTools may not work if there is more than one instance of React in the bundle. This is often related with an incorrect bundling tool configuration (browserify or webpack) in a particular component. React components (and other libraries supported by React, such as Mixins) should always have react as a peerDependency in npm and as an "external" in browserify/webpack. This will make it so that a distributable version of the module will not embed React for itself. In addition, if React add-ons are used, "react/addons" may also have to be registered as an external dependency.

In cases where this was not followed, the mere inclusion of the module with require (or ES6's import) will render the dev tools useless. When another instance of React emerges, that one will register itself as the source of the element tree, which is why an empty element is shown. I have tracked down this bug in react-loaders (issue #2), and it's now fixed since 1.2.3. The same might have happened to react-google-maps according to what @Carpetfizz said, although I have not looked deeply into this case.

One easy trick to debug this is to take a barebones module + web page and iteratively add require instructions until the React dev tools stop working. Once the faulty component is found, file an issue.

var React = require('react');
var Loader = require('react-loaders'); // testing react-loaders

var App = React.createClass({
    render: function(){
        return (
            <div>
               <p>Check the React tab!</p>
            </div>
        )
    }
});

React.render(<App />, document.getElementById('container'));

Another peculiar solution was carried out in the React packages for Meteor, where the development runtime was changed to load the main instance of React last (commit).

This topic was lifted in issue #90, which helped identify more cases of non-compliant packages.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!