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.

How to destroy root Preact node?

See original GitHub issue

I want to destroy the root Preact DOM node. I initially render my component as follows:

import { h, render } from 'preact';
import App from "./components/App";

render(<App />, document.querySelector("#app");

How do I destroy App? Do I simply unmount the #app DOM node, or does Preact offer a method similar to React’s unmountComponentAtNode() method?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
Kanayecommented, Jun 20, 2018

To destory your app properly you can render null or ''(empty string) into your container and set the third parameter merge to your apps root element. Preact will then clean up the dom (okay it will leave an empty TextNode).

Example:

class App extends Component {
  render() {
    return (<h1> Some app </h1>);
  }

  componentWillUnmount() {
    window.alert('bye cruel world');
  }
}


const root = render(<App />, document.querySelector('#app'));

function destroy() {
  render(null, document.querySelector('#app'), root);
}

Here the example: https://jsfiddle.net/8pcfntvz/4/

5reactions
marvinhagemeistercommented, Jan 26, 2021

@cattermo It’s correct that render doesn’t return anything, but it did in older Preact versions during the 8.x release line. In current Preact versions (>= 10) the third argument can and should be omitted. We’re planning to drop it in the next major version.

To destroy a root node in current Preact versions (>= 10) calling

render(null, document.querySelector('#app'))

does the job.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to destroy root Preact node? - Stack Overflow
I want to destroy the root Preact DOM node. I initially render my component as follows: import { h, render } from 'preact';...
Read more >
How to destroy root Preact node? · Issue #1151 - GitHub
To destory your app properly you can render null or '' (empty string) into your container and set the third parameter merge to...
Read more >
API Reference | Preact
If you've already pre-rendered or server-side-rendered your application to HTML, Preact can bypass most rendering work when loading in the browser. This can...
Read more >
[Solved]-How to destroy root Preact node?-Reactjs
Excerpt from compat for Preact X function unmountComponentAtNode(container) { if (container._prevVNode!=null) { render(null, container); return true; } ...
Read more >
Preact-cli NPM
You can disable default: true flags by prefixing them with --no-<option> ; for example, --no-sw , --no-esm , and --no-inline-css . $ preact...
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