[英]React element change state from dom
I have been using this function: 我一直在使用此功能:
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
});
To hover over elements in our website but since React 16, this is broken with an error 为了将鼠标悬停在我们网站上的元素上,但是从React 16开始,它被错误破坏
'VM25001:1 Uncaught TypeError: Cannot read property '_owner' of undefined at window.FindReact (:1:181) at :3:18'
'VM25001:1未被捕获的TypeError:无法在window:FindReact(:1:181)处读取未定义的属性'_owner':3:18'
How can I fix this? 我怎样才能解决这个问题?
You can create the logic to manipulate state from the DOM from inside React: 您可以在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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.