Javascript 使用React和Bacon从节点获取EventStream
这就是我如何使用bacon.js从常规DOM节点获取EventStream的方法:Javascript 使用React和Bacon从节点获取EventStream,javascript,reactjs,frp,bacon.js,Javascript,Reactjs,Frp,Bacon.js,这就是我如何使用bacon.js从常规DOM节点获取EventStream的方法: var el = document.getElementById('input1'); var stream = Bacon.fromEvent(el, 'input') 使用React时,DOM节点可能会在一些render()迭代后重新创建,因此从真实的DOM节点(使用React.findDOMNode)获取事件不是一个选项。目前我能想到的唯一选择是手动处理事件,然后将其推送到总线: var Componen
var el = document.getElementById('input1');
var stream = Bacon.fromEvent(el, 'input')
使用React时,DOM节点可能会在一些render()迭代后重新创建,因此从真实的DOM节点(使用React.findDOMNode)获取事件不是一个选项。目前我能想到的唯一选择是手动处理事件,然后将其推送到总线:
var Component = React.createClass({
streams: {},
componentDidMount: function() {
this.streams.inputBus = new Bacon.Bus();
},
componentWillUnmount: function() {
this.streams.inputBus.end();
},
onInput: function(e) {
this.streams.inputBus.push(e);
},
render: function() {
return (
<input type="text" onInput={this.onInput} />
);
}
});
var Component=React.createClass({
流:{},
componentDidMount:function(){
this.streams.inputBus=new Bacon.Bus();
},
componentWillUnmount:function(){
this.streams.inputBus.end();
},
onInput:函数(e){
this.streams.inputBus.push(e);
},
render:function(){
返回(
);
}
});
这种方法行吗,或者有更好的解决方案吗?我想说它行。在这两个示例中使用了类似的方法: