admin管理员组

文章数量:1313313

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

import { h, render } from 'preact';
import App from "./ponents/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?

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

import { h, render } from 'preact';
import App from "./ponents/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?

Share Improve this question asked Jun 20, 2018 at 11:16 Daniel AptDaniel Apt 2,6581 gold badge23 silver badges35 bronze badges 1
  • Please mark an answer as accepted – marvinhagemeister Commented Mar 8, 2019 at 10:00
Add a ment  | 

2 Answers 2

Reset to default 11

Disclaimer: I work on preact.

Any rendered preact app can be easily destroyed by passing null to render:

render(null, document.querySelector("#app"));

We don't need any special functions to do that and have chosen to keep a small API surface area. The implementation for unmountComponentAtNode in preact-pat does literally just call render with null:

// Excerpt from pat for Preact X
function unmountComponentAtNode(container) {
    if (container._prevVNode!=null) {
        render(null, container);
        return true;
    }
    return false;
}

There is no method like unmountComponentAtNode() without preact-pat. A workaround is to use the third argument of the render() method to replace the ponent you want to unnmount by '' or null like suggested here : https://github./developit/preact/issues/53#issuement-184868295

本文标签: javascriptHow to destroy root Preact nodeStack Overflow