【问题标题】:Get EventStream from a node with React and Bacon使用 React 和 Bacon 从节点获取 EventStream
【发布时间】:2015-09-02 11:30:09
【问题描述】:

这就是我可以使用 bacon.js 从常规 DOM 节点获取 EventStream 的方法:

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} />
    );
  }
});

这种方法可以吗,还是有更好的解决方案?

【问题讨论】:

    标签: javascript reactjs frp bacon.js


    【解决方案1】:

    我会说没关系。这两个示例中使用了类似的方法:

    【讨论】:

      猜你喜欢
      • 2015-02-25
      • 1970-01-01
      • 2015-02-22
      • 2015-06-16
      • 1970-01-01
      • 1970-01-01
      • 2017-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多