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.

10.0.0-beta.1 componentWillUnmount does not fire when using Preact.render() with replaceNode

See original GitHub issue

When calling preact.render() with the third optional replaceNode argument, re-rendering over a component does not call the componentWillUnmount lifecycle hook.

Consider the HTML:

<div id='app'>
  <div id='component'></div>
</div>

I want to change what component is rendered in the #component div, and because our app is not 100% preact, we use a separate call to preact.render to swap out the components.

First render ComponentA: render(<ComponentA />, document.getElementById('app'), document.getElementById('component'));

Resulting in:

<div id='app'>
  <div id='component'>I am ComponentA</div>
</div>

Sometime later render over ComponentA with ComponentB: render(<ComponentB />, document.getElementById('app'), document.getElementById('component'));

Which does actually render correctly resulting in:

<div id='app'>
  <div id='component'>I am ComponentB</div>
</div>

But ComponentA did not call componentWillUnmount before it was overwritten. In my specific case this causes a setInterval to go uncleared, and results in some strange behavior when that interval continues to be called.

I have a codesandbox which shows the behavior more clearly: https://codesandbox.io/s/preactwillunmountbug-fi1d5

If you open a console and click the Overwrite clock doesn't call willUnmount button you will see the interval continue to log statements into the console. At first the clock isn’t present, and then it gets appended next to the other placeholder component, I think as a result of the call to setState inside the interval.

If you instead click the Unmount clock calls willUnmount button you will see the unmount functional runs and the interval is cleared.

It was my expectation that both of these methods of overwriting a component with another should result in the same behavior.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
LukasBombachcommented, May 22, 2019

@1campbellj I am currently writing on a lengthy medium article about this which I hope to publish today. Along with the article I have implemented a working proof of concept that does partial hydration with preact and next.

You can already check out the repo here: https://github.com/spring-media/next-super-performance

Read more comments on GitHub >

github_iconTop Results From Across the Web

API Reference | Preact: Fast 3kb React alternative with the ...
The render() function is required for all components. It can inspect the props and state of the component, and should return a Preact...
Read more >
Preact - Releases
Today we are announcing a new hook: useId . This hook creates stable unique identifiers that are consistent between server-side rendering (using preact-render- ......
Read more >
Why doesn't componentWillUnmount trigger when closing the ...
According to the docs, componentWillUnmount() is triggered when unmounting it or if it's relative component is being destroyed.
Read more >
Preact NPM - npm.io
Note: You don't need ES2015 to use Preact... but give it a try! ... Components also have a render() method, but unlike React...
Read more >
The Zen of Preact's source code // Puru Vijay - puruvj.dev
I don't need to say this out loud really, but Preact's source code is very ... trigger re-renders and are not re-created on...
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