gpt4 book ai didi

javascript - 错误 : "Attempted to access ReactWrapper::node, which was previously a private property" when using enzyme 3

转载 作者:行者123 更新时间:2023-11-29 23:28:27 25 4
gpt4 key购买 nike

我的测试中有以下几行(使用 react 和 enzyme ):

const input = mount(<MyComponent/>).find('input').node;
input.focus();
input.select();

我已将 enzyme 从 v2 升级到 v3,现在它抛出以下错误:

Attempted to access ReactWrapper::node, which was previously a private property on Enzyme ReactWrapper instances, but is no longer and should not be relied upon. Consider using the getElement() method instead.

但是,当我使用 getElement()就像错误中建议的那样:

const input = mount(<MyComponent/>).find('input').getElement();

生成的对象没有原始 .node 中的功能:

TypeError: (0 , _enzyme.mount)(...).find(...).getElement(...).focus is not a function

TypeError: (0 , _enzyme.mount)(...).find(...).getElement(...).select is not a function

我应该用什么代替 .node.getElement()使此代码在 enzyme 3 中起作用?

最佳答案

您应该使用 .instance().getDOMNode() 而不是 .node,这取决于您是否将结果用作 ReactElement或 DOMComponent。

关于javascript - 错误 : "Attempted to access ReactWrapper::node, which was previously a private property" when using enzyme 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48204668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com