加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – React 13.3 unmountComponentAtNode()错误:目标容器

发布时间:2020-12-15 05:04:51 所属栏目:百科 来源:网络整理
导读:当我从React 12.2升级到React 13.3时,我的测试套件中出现以下错误: Error: Invariant Violation: unmountComponentAtNode(…): Target container is not a DOM element. 我正在使用this blog post使用Jasmine测试我的代码.此代码中发生错误: describe("A c
当我从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()时,它返回给定的实例,但是当它出错时,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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读