在孩子:
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