question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Preact flushing entire components using react-hot-loader

See original GitHub issue

Hi guys,

I had a tough day trying to make preact work with react-hot-loader v3 but at least I have discovered why.

The problem I had was that preact was flushing an entire form component when typing on an input, it was supposed to just re-render the input. Unmounting and mounting the component make the input lose the focus and the form is not usable anymore.

After much investigating the problem was that react-hot-loader uses proxies in order to create instances of the components, see: https://github.com/gaearon/react-hot-loader/blob/master/src/patch.dev.js#L106

Using RHL, when a component is created using inst = new Ctor(props, context) by preact we are not getting an instance of Ctor, but an instance of a copy of Ctor created by the proxy.

That makes that inst.constructor !== Ctor so the next time that the component get rendered, preact tries to build it from the VNode and finds that dom._componentConstructor !== vnode.nodeName and it decides to unmount it.

I understand this is not preact’s fault, but it’s so easy to fix just modifying one line of code of preact, overriding inst.constructor = Ctor for all components:

In component-recycler.js:

if (Ctor.prototype && Ctor.prototype.render) {
  inst = new Ctor(props, context);
  Component.call(inst, props, context);
} else {
  inst = new Component(props, context);
  inst.render = doRender;
}

// Override the constructor even if the instance was created by `new Ctor()`
// This will make preact work ok with react-hot-loader using proxies to create instances
inst.constructor = Ctor;

If I create a PR would you merge it? It’s my missing piece to finally make preact and react-hot-module work together!

Thanks for preact, it’s great!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:30 (15 by maintainers)

github_iconTop GitHub Comments

2reactions
marvinhagemeistercommented, Aug 10, 2020

FYI: We now have an official HMR solution for Preact called prefresh. We recommend everyone switching over to use that 🎉

2reactions
arqexcommented, May 1, 2018

@theKashey it’s the opposite, it’s working really well with v4. Just returning an empty stack from getReactStack. Preact re-renders the dom nicely, preserving the state.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prefresh, Fast-refresh for Preact - DEV Community ‍ ‍
Hot module reloading is a great way to improve developer experience, hitting save and seeing the output with the snap of a finger...
Read more >
Set up React Hot Loader in 10 minutes - LogRocket Blog
A hot reload to an app will only refresh the files that were changed without losing the application's state. A live reload to...
Read more >
@stoplight/react-static - npm package | Snyk
Hot Reloadable out-of-the-box. Edit React components & styles in real-time. LAN accessible dev environment for testing on other devices like phones and ...
Read more >
Preact - Releases
Preact - ⚛️ Fast 3kB React alternative with the same modern API. Components & Virtual DOM. - Releases.
Read more >
Getting Started · React Hot Loader - Dan gaearon
React Hot Loader is a plugin that allows React components to be live reloaded without the loss of state. It works with Webpack...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found