我正在制作并监听普通 DOMCustomEvent
s 与父节点通信:
儿童时期:
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 的方法会更好呢?
如上所述:
React 的方式是通过 props 显式地将回调传递给子级。 React 中不支持带有冒泡的自定义事件。
响应式编程抽象是正交的:
通过观察者模式对交互式系统进行编程是困难且容易出错的,但仍然是许多生产环境中的实现标准。我们提出了一种逐渐弃用观察者而支持反应式编程抽象的方法。多个库层可帮助程序员将现有代码从回调平滑迁移到更具声明性的编程模型。
React 哲学基于命令模式:
参考
- 弃用观察者模式 http://lambda-the-ultimate.org/node/4028
- 命令模式:命令历史记录 http://www.cs.mcgill.ca/~hv/classes/CS400/01.hchen/doc/command/command.html
- 与 React 和自定义元素的组件互操作 http://addyosmani.com/blog/component-interop-with-react-and-custom-elements/
- 在 TypeScript 中构建 Redux http://blog.ng-book.com/introduction-to-redux-with-typescript-and-angular-2/
- Mithril 与其他框架有何不同 - Mithril http://mithril.js.org/comparison.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)