Skip to content

Reload on Save is triggering full HMR reload #402

Closed
@gnoff

Description

@gnoff

Hi It's been a while since I have set up a new React project with HMR functionality so I apologize if this is known and/or expected.

When I create-react-app new-app and npm start updates to App.js are reloaded but the webpack HMR is doing a full reload.

My specific steps were

  1. create new app with create-react-app
  2. start app and visit index with npm start (browser window opened automatically
  3. Changed App.js line 14:
To get started, edit <code>src/App.js</code> and save to reload.

to

To get started, edit <code>src/App.js</code> and save to reload.............
  1. reviewed the browser console and saw:
[WDS] App updated. Recompiling...
client:90 [WDS] App hot update...
dev-server.js:55 [HMR] Checking for updates on the server...
dev-server.js:18 [HMR] Cannot apply update. Need to do a full reload!
dev-server.js:19 [HMR] Error: Aborted because 256 is not accepted
    at hotApply (http://localhost:3000/static/js/bundle.js:391:31)
    at hotUpdateDownloaded (http://localhost:3000/static/js/bundle.js:304:13)
    at hotAddUpdateChunk (http://localhost:3000/static/js/bundle.js:284:13)
    at webpackHotUpdateCallback (http://localhost:3000/static/js/bundle.js:5:12)
    at http://localhost:3000/0.0234ed6f7496ad680301.hot-update.js:1:1

I know there are some limitations with HMR and react hot reloader so if this is expected per the configuration then I'll quickly close this issue but it was surprising behavior to me

Activity

gaearon

gaearon commented on Aug 8, 2016

@gaearon
Contributor

There is by design.
(I know it’s confusing because of WDS logs: #263)

Hot reloading is currently only supported for CSS. React Hot Loader is not included in this setup because it’s not stable enough to be recommended as a default setup for everyone. If it’s ever stable enough, we will include it in the future.

gnoff

gnoff commented on Aug 8, 2016

@gnoff
Author

Thanks I figured it was something like this.

aleemb

aleemb commented on Aug 10, 2016

@aleemb

@gaearon hot reload is the primary reason I came looking here. If you could have a dev channel with hot reload, a lot of people would find it useful.

gaearon

gaearon commented on Aug 10, 2016

@gaearon
Contributor

One step at a time 😉

srhise

srhise commented on Sep 20, 2016

@srhise

So, you have to eject to get HMR working at the component level. Just making sure.

gaearon

gaearon commented on Sep 21, 2016

@gaearon
Contributor

Technically you can write a top-level module.hot.accept handler in src/index.js and re-render your app with a new version of <App> component on change but that will reset the internal component state. For granular updates, yes, currently you would have to eject.

aleemb

aleemb commented on Sep 21, 2016

@aleemb

For my purposes, I added React StoryBook and was able to get HMR. It has the added advantage of showing the errors directly on the screen which has better visibility than having to look at the CLI manually for errors.

image

gaearon

gaearon commented on Sep 21, 2016

@gaearon
Contributor

@aleemb If you’d like, you can contribute “Adding Storybook” to the Usage Guide.

dzannotti

dzannotti commented on Sep 29, 2016

@dzannotti

For those reaching here from google, you can technically add

if (module.hot) {
  module.hot.accept('./App', function () {
    var NextApp = require('./App').default
    ReactDOM.render(<NextApp />, document.getElementById('root'))
  })
}

to your src/App.js and it'll work. Please do note that HMR for React/dom is not stable yet, and has downsides (which is why it's not enabled by default).

💀 use at your own risk 💀

(imho still better than ejecting)

gaearon

gaearon commented on Sep 29, 2016

@gaearon
Contributor

It will work but not preserve the state, which is not much better than a full refresh. Depends on whether you use something like Redux I guess 😄 .

jonstuebe

jonstuebe commented on Nov 3, 2016

@jonstuebe

@gaearon by not preserving state, do you mean it's re-mounting components and thus resetting state?

bordi

bordi commented on Feb 1, 2017

@bordi

@jonstuebe exactly. it will reset internal state

cubodehelio

cubodehelio commented on Mar 5, 2017

@cubodehelio

I'm using redux with react, but I cant find any modern example on how to use it with hmr without loosing the redux state :(

gaearon

gaearon commented on Mar 5, 2017

@gaearon
Contributor

@cubodehelio What have you tried?

cubodehelio

cubodehelio commented on Mar 8, 2017

@cubodehelio

hey @gaearon it turns out that i'm still learning here, sometimes everything works just fine and others it doesn't, normal stuff. Thanks for you work!

gaearon

gaearon commented on Mar 9, 2017

@gaearon
Contributor

If you create a new issue describing what you've tried (and maybe attaching a project) we could help.

fingermark

fingermark commented on Apr 27, 2017

@fingermark

Was surprised to find that hot component reloading didn't work out of the box. Should have read the README. Would it be possible to have this enabled with an experimental flag?

gaearon

gaearon commented on Apr 27, 2017

@gaearon
Contributor

No until it is implemented in a proper stable way.

We are adding the error box in 0.10 which is a good first step towards this. We will look at enabling it for functional components at some point after that.

AlexanderOtavka

AlexanderOtavka commented on Jun 4, 2017

@AlexanderOtavka

@dzannotti I tried your snippet, and while it works for regular react-scripts, it doesn't work when using react-scripts-ts. It looks like index.tsx gets wrapped in webpackHotUpdate() and the callback in module.hot.accept never gets called.

I don't really understand how hot works. Any advice?

locked and limited conversation to collaborators on Jan 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @aleemb@jonstuebe@gaearon@srhise@fingermark

        Issue actions

          Reload on Save is triggering full HMR reload · Issue #402 · facebook/create-react-app