拆除测试时使用unmountComponentAtNode或document.body.removeChild



从这些文档:https://reactjs.org/docs/hooks-faq.html#how-测试使用挂钩的组件

文档提供了一种设置和拆除测试的方法,如下所示:

let container;
beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
});
afterEach(() => {
document.body.removeChild(container);
container = null;
});

在此文档中:https://reactjs.org/docs/testing-recipes.html#setup--拆卸

设置和拆卸方式如下:

import { unmountComponentAtNode } from "react-dom";
let container = null;
beforeEach(() => {
// setup a DOM element as a render target
container = document.createElement("div");
document.body.appendChild(container);
});
afterEach(() => {
// cleanup on exiting
unmountComponentAtNode(container);
container.remove();
container = null;
});

我有点困惑,哪一种是最好的方式来结束测试?

unmountComponentAtNode+dom.remove()还是document.body.removeChild

更新

这两份官方文件在分解测试时给出了这两种方法,这是否意味着它们都可以?它们等效吗?还是怎样

unmountComponentAtNode将调用componentWillUnmount生命周期方法,但document.body.removeChild不会。

最新更新