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

reactjs – react.js自定义事件,用于与父节点通信

发布时间:2020-12-15 06:50:39 所属栏目:百科 来源:网络整理
导读:我正在制作和监听正常的DOM CustomEvents与父节点通信: 在孩子: var moveEvent = new CustomEvent('the-graph-group-move',{ detail: { nodes: this.props.nodes,x: deltaX,y: deltaY },bubbles: true }); this.getDOMNode().dispatchEvent(moveEvent); 在
我正在制作和监听正常的DOM CustomEvents与父节点通信:

在孩子:

var moveEvent = new CustomEvent('the-graph-group-move',{ 
    detail: {
      nodes: this.props.nodes,x: deltaX,y: deltaY
    },bubbles: true
  });
  this.getDOMNode().dispatchEvent(moveEvent);

在父代:

componentDidMount: function () {
  this.getDOMNode().addEventListener("the-graph-group-move",this.moveGroup);
},

这工作,但有一个React特定的方式,会更好吗?

如上所述:

The React way would be to pass callbacks down to children explicitly via props — . There’s no support for custom events w/ bubbling in React.

反应式编程抽象是正交的:

Programming interactive systems by means of the observer pattern is hard and error-prone yet is still the implementation standard in many production environments. We present an approach to gradually deprecate observers in favor of reactive programming abstractions. Several library layers help programmers to smoothly migrate existing code from callbacks to a more declarative programming model.

React的理念是基于Command模式:

参考文献

> Deprecating the Observer Pattern
> Command Pattern: Command History
> Component Interop with React and Custom Elements
> Building Redux in TypeScript
> How is Mithril Different from Other Frameworks – Mithril

(编辑:李大同)

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

    推荐文章
      热点阅读