反应事件目标父节点

2024-03-16

是否可以获取虚拟 DOM 上事件目标的父节点?

在我的基本 React 组件中,我有一个从 onClick 事件触发的方法,我想获取父虚拟 DOM 节点属性。

handleClick(e){ 
    // The following code only gives me the actual DOM parent Node
    ReactDOM.findDOMNode(e.target).parentNode 

}

是的。 React 在虚拟 DOM 模型上工作,并且仅在需要时才会更新真实的浏览器 DOM。 React 中的事件也在虚拟 DOM 上工作,它们被称为合成的 https://facebook.github.io/react/docs/events.html事件。如果您发现出于某种原因需要底层浏览器级事件,只需使用nativeEvent属性即可获取it https://facebook.github.io/react/docs/events.html.

React Virtual DOM 中有一些限制,您可以从中读取here https://facebook.github.io/react/docs/handling-events.html:

Also, this http://reactkungfu.com/2015/10/the-difference-between-virtual-dom-and-dom/可能会有所帮助,并解释了虚拟 DOM 和浏览器 DOM 之间的区别。


浏览器 DOM 只是页面元素的抽象,而 React DOM 将是该抽象的抽象。

The render的方法ReactDOM至关重要的是我们如何将 React 元素发送到浏览器 DOM:

var React = require('react');
var ReactDOM = require('react-dom');
var root = React.createElement('div');
ReactDOM.render(root, document.getElementById('example'));
        ^ 
        |---- Where React elements from React Virtual DOM enter the Browser DOM

在 React 中,我们正在处理 React 元素和 React 组件。

由于 React Element 是 DOM 元素的虚拟表示,因此要处理事件并捕获父元素,您需要通过以下三种方式之一创建 React 组件:

例如,要在单击事件中导航父级,您可以检查以下内容:

 var parent = this._reactInternalInstance._currentElement._owner._instance;

REF: React 有没有办法访问父组件实例? https://stackoverflow.com/questions/34257665/is-there-any-way-to-access-the-parent-component-instance-in-react and http://jsfiddle.net/BinaryMuse/j8uaq85e/ http://jsfiddle.net/BinaryMuse/j8uaq85e/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

反应事件目标父节点 的相关文章

随机推荐