简体   繁体   English

Electron、React 和 Webpack,如何让 HMR 发挥作用?

[英]Electron, React and Webpack, how to make HMR work?

It is hard to comprehend the complexities of webpack and I need some help getting HMR to work, I have started a new project using electron-forge using the typescript-webpack template.很难理解 webpack 的复杂性,我需要一些帮助才能让 HMR 工作,我已经开始了一个使用电子伪造的新项目,使用 typescript-webpack 模板。

The project got created with the following structure:该项目是使用以下结构创建的: 项目

You can see the ts-loader configuration is already in there, afterwards I added react and react-dom and set up a hello world App component, the problem is now, when I change that component, I get a webpack HMR error:您可以看到 ts-loader 配置已经在那里,之后我添加了 react 和 react-dom 并设置了一个 hello world App 组件,现在的问题是,当我更改该组件时,出现 webpack HMR 错误:

电子误差

not sure how to solve this issue, react-hot-loader seems to be the library to go to get this working but not sure how to make it work with ts-loader, could anybody point out how to get this set up working?不知道如何解决这个问题,react-hot-loader 似乎是让这个工作的库,但不确定如何让它与 ts-loader 一起工作,有人能指出如何让这个设置工作吗? Thanks!谢谢!

Edit 1: I tried adding react-hot-reload but I get a require is not defined error...编辑 1:我尝试添加 react-hot-reload 但我得到一个require is not defined错误......

在此处输入图片说明

Well, after fumbling around with the react-hot-loader package I just could not get it to work, so I stumbled upon the default webpack hot reloading way:好吧,在摸索着react-hot-loader包之后,我就是无法让它工作,所以我偶然发现了默认的 webpack 热重载方式:

on the index file (where you import your root react component, you can use something like:在索引文件上(在其中导入根 React 组件,您可以使用以下内容:

declare let module: { hot: any };

if (module.hot) {
  module.hot.accept("./App", () => {
    const NewApp = require("./App").default;

    render(<NewApp />, document.getElementById("app"));
  });
}

At least it seems to be working so far至少到目前为止它似乎有效

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

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