简体   繁体   English

Webpack require.ensure(代码拆分)与React组件不起作用?

[英]Webpack require.ensure(code splitting) with react component not working?

I have the below component. 我有以下组件。 It is not rendering the product module in the dom and also not showing any error in the console. 它不会在dom中呈现产品模块,也不会在控制台中显示任何错误。

And if I use ReactDOM.render(<ProductModule/>,document.getElementById('product-container')); 如果我使用ReactDOM.render(<ProductModule/>,document.getElementById('product-container')); it is working. 这是工作。

import React, {Component} from 'react';
import ReactDOM from 'react-dom';
class ProductModuleWrapper extends Component {
    constructor(props) {
    }

    render() {
        return (
            <div className="product-container">
                {this.renderProductModules()}
            </div>
        );
    }

    renderProductModules() {
        require.ensure([],(require) => {
            var ProductModule =   require('../ProductModule').default;
            return ProductModule;
        },'productmodule');
    }
}

edit : 编辑:

I think this is something to do with the async nature of the require ensure call, Please help 我认为这与require sure调用的异步性质有关,请帮助

React will only re-render if component's state or props have been updated. 只有在组件的状态或道具已更新时,React才会重新渲染。 In your example, neither is so it won't render the component as it's fetched asynchronously. 在您的示例中,两者都不是,因此它不会呈现组件,因为它是异步获取的。

I recommend that you use this.state as described in this blog post: http://blog.netgusto.com/asynchronous-reactjs-component-loading-with-webpack/ 我建议您按照此博客文章中所述使用this.statehttp : //blog.netgusto.com/asynchronous-reactjs-component-loading-with-webpack/

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

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