electron-userland / electron-userland/electron-webpack

How to get real HMR working with React-hot-loader

Open
#59 14 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
902
Forks
168
PR merge metrics
No merged PRs in 30d

Description

Hi!

Just starting a new app with this library. While it's smooth, it's maybe a bit TOO smooth as I had to jump through a lot of hoops to get real HMR working since you abstract away the babel config.

So here's what I did:

1. Since only presets starting with "babel-preset-" are automatically included, and I found no way to include them otherwise, the react-hot-loader preset is impossible to use. To fix this I created a new babel preset called `babel-preset-react-hot-loader` that just adds the babel stuff from React Hot Loader. Just install that and a big part of the work is done. You're welcome.

2. My `renderer/index.js` file looks like this, copied mostly from the `react-hot-loader` example:

```
import React from 'react'
import ReactDOM from 'react-dom'
import { AppContainer } from 'react-hot-loader'
import Root from './Root'

const render = Component => {
ReactDOM.render(


,
document.getElementById('app'),
)
}

render(Root)

// Webpack Hot Module Replacement API
if( module.hot ) {
module.hot.accept('./Root', () => { render(Root) })
}
```

Aaaaand it seems to work! State is now retained across hot replacements. I did not need to do the Webpack stuff that's mentioned in the `react-hot-loader` setup instructions, but maybe that will prove necessary in the future.

I think it's a bit misleading to claim that `electron-webpack` is HMR enabled, but then not provide any kind of documentation on how to use it. I had to make a new package just to add a babel preset to make it work with the most popular HMR tool for React. Out of the box state is not kept across hot replacements. Hope this helps someone!

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.