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?
- Please mark an answer as accepted – marvinhagemeister Commented Mar 8, 2019 at 10:00
2 Answers
Reset to default 11Disclaimer: 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
版权声明:本文标题:javascript - How to destroy root Preact node? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741934187a2405751.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论