我想销毁根Preact DOM节点。我最初呈现我的组件如下所示:
import { h, render } from 'preact';
import App from "./components/App";
render(<App />, document.querySelector("#app");如何销毁App?我是简单地卸载Preact DOM节点,还是提供一个类似于React的unmountComponentAtNode()方法的方法?
发布于 2019-02-24 22:45:27
免责声明:我在preact上工作。
通过将null传递给render,可以轻松销毁任何呈现的preact应用程序
render(null, document.querySelector("#app"));我们不需要任何特殊的函数来做到这一点,并选择保留一个很小的API外围应用。preact-compat中的unmountComponentAtNode实现实际上只是使用null调用render
// Excerpt from compat for Preact X
function unmountComponentAtNode(container) {
if (container._prevVNode!=null) {
render(null, container);
return true;
}
return false;
}发布于 2018-12-21 00:18:53
没有预压缩,就没有像unmountComponentAtNode()这样的方法。一种解决方法是使用render()方法的第三个参数,将要卸载的组件替换为''或null,如下所示:https://github.com/developit/preact/issues/53#issuecomment-184868295
https://stackoverflow.com/questions/50946950
复制相似问题