从dom

时间:2018-01-12 20:52:50

标签: javascript reactjs browser-automation

我一直在使用这个功能:

window.FindReact = function (dom) {
  for (var key in dom) {
    if (key.startsWith('__reactInternalInstance$')) {
      var compInternals = dom[key]._currentElement;
      var compWrapper = compInternals._owner;
      var comp = compWrapper._instance;
      return comp;
    }
  }
  return null;
};
var someElement = $x("someElement")[0];
window.FindReact(someElement).setState({
  hover: true
});

将鼠标悬停在我们网站上的元素上,但自从React 16以来,这已经错误

  

'VM25001:1未捕获的TypeError:无法在window.FindReact(:1:181)处读取未定义的属性'_owner':3:18'

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

你可以创建逻辑来从React里面的DOM操作状态:

constructor() {
  // A function to manipulate the state:
  this.changeState = this.changeState.bind(this);

  const domElementToManipulateState = document.getElementById('domElement');

  domElementToManipulateState.addEventListener('hover', this.changeState);
}