简体   繁体   English

通过Babel的Webpack和带有Es2015模块的React

[英]Webpack & React With Es2015 Modules via Babel

Im really struggling with Webpack & React. 我真的在Webpack&React上挣扎。 It seems for ages, Node 5.0 was not registering the npm modules I installed in the package.json 'dependencies'. 似乎很久以来,Node 5.0并未注册我在package.json'dependencies'中安装的npm模块。 Today, I upgraded Node to 5.2 and it seems to be chugging along fine now. 今天,我将Node升级到5.2,现在看来一切都很好。

However, now Im struggling with getting React code in modules to work. 但是,现在我正在努力使模块中的React代码能够正常工作。

Webpack doesn't report any error. Webpack不报告任何错误。 Chrome Console says: Chrome控制台说:

"Uncaught Error: Invariant Violation: ReactDOM.render(): Invalid component element." “未捕获的错误:始终违反:ReactDOM.render():无效的组件元素。” (which is pointing to Main.jsx file). (指向Main.jsx文件)。

There is a lot of dependencies & my webpack config is fairly lengthy so I hesitantly post the main.jsx file content here only on SO. 有很多依赖关系,我的webpack配置相当冗长,因此我只在SO上犹豫地将main.jsx文件内容发布在这里。 The whole repo (with length config files, however, the React code itself is a few lines long) is here on Bitbucket . 整个仓库(带有长度的配置文件,但是,React代码本身只有几行) 位于Bitbucket上

main.jsx: main.jsx:

import React from 'react';
import ReactDOM from 'react-dom';
import AppParent from './components/App.jsx';
main ();
function main () {
    const app = document.createElement ('div');
    document.body.appendChild (app);
    ReactDOM.render (AppParent, app);
};

App.jsx: App.jsx:

import React from 'react';
import Topbar from './Topbar.jsx';
export default AppParent;
var AppParent = React.createClass ({
    render : function () {
        return (
            <div className = 'ReactParent'>
            </div>
        );
    }
});

In the render method you need to render an element: render方法中,您需要渲染一个元素:

ReactDOM.render (<AppParent />, app);

You are passing a React component class . 您正在传递一个React组件类

您不渲染的元素应该是ReactDom.render(<AppParent />, app)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM