React 13.3 unmountComponentAtNode() Error: Target container is not a DOM element
React 13.3 unmountComponentAtNode() Error: Target container is not a DOM element
当我从 React 12.2 升级到 React 13.3 时,我的测试套件出现以下错误:
Error: Invariant Violation: unmountComponentAtNode(...): Target
container is not a DOM element.
我正在使用 this blog post 来使用 Jasmine 测试我的代码。这段代码出现错误:
describe("A component", function() {
var instance;
var container = document.createElement("div");
afterEach(function() {
if (instance && instance.isMounted()) {
// Only components with a parent will be unmounted
React.unmountComponentAtNode(instance.getDOMNode().parent);
}
});
...rest of test suite...
// the instances of renderIntoDocument that cause the failure look like the below
it("Causes my test suite to fail.", function() {
instance = TestUtils.renderIntoDocument(<MyReactElement/>);
});
)};
我知道 getDOMNode()
已被弃用,但这不是导致错误的原因。
当我检查 instance.getDOMNode()
时,它 returns 给定的实例,但是当它出错时, instance.getDOMNode().parent
是未定义的。在此未定义的变量上调用 React.unmountComponentAtNode
导致错误。
像 this one 这样的回答表明存在某种竞争条件,但我不确定这将如何应用于我的测试套件。感谢您的帮助!
解决方法是更改:
React.unmountComponentAtNode(instance.getDOMNode().parent);
至:
React.unmountComponentAtNode(instance.getDOMNode().parentNode);
或者,如果您要从 getDOMNode()
移动到 findDOMNode()
:
React.unmountComponentAtNode(React.findDOMNode(instance).parentNode);
当我从 React 12.2 升级到 React 13.3 时,我的测试套件出现以下错误:
Error: Invariant Violation: unmountComponentAtNode(...): Target container is not a DOM element.
我正在使用 this blog post 来使用 Jasmine 测试我的代码。这段代码出现错误:
describe("A component", function() {
var instance;
var container = document.createElement("div");
afterEach(function() {
if (instance && instance.isMounted()) {
// Only components with a parent will be unmounted
React.unmountComponentAtNode(instance.getDOMNode().parent);
}
});
...rest of test suite...
// the instances of renderIntoDocument that cause the failure look like the below
it("Causes my test suite to fail.", function() {
instance = TestUtils.renderIntoDocument(<MyReactElement/>);
});
)};
我知道 getDOMNode()
已被弃用,但这不是导致错误的原因。
当我检查 instance.getDOMNode()
时,它 returns 给定的实例,但是当它出错时, instance.getDOMNode().parent
是未定义的。在此未定义的变量上调用 React.unmountComponentAtNode
导致错误。
像 this one 这样的回答表明存在某种竞争条件,但我不确定这将如何应用于我的测试套件。感谢您的帮助!
解决方法是更改:
React.unmountComponentAtNode(instance.getDOMNode().parent);
至:
React.unmountComponentAtNode(instance.getDOMNode().parentNode);
或者,如果您要从 getDOMNode()
移动到 findDOMNode()
:
React.unmountComponentAtNode(React.findDOMNode(instance).parentNode);