如何让React.js组件在Reflux中监听商店

psv*_*svj 4 reactjs refluxjs

我正在使用React.js和Reflux构建一个应用程序,我无法获取组件来收听商店.

首先,我已成功将我的商店与一个事件相关联,如下所示:

组件发送操作以存储:

var CalcRow = React.createClass({
  handleChange: function(){
        // links to action in store.js
        TodoActions.costChange();
  },
  render: function() {
        return(// redacted)
    }
});
Run Code Online (Sandbox Code Playgroud)

行动:

global.TodoActions = Reflux.createActions([
    "costChange"  // called by individual cost item input    
]);
Run Code Online (Sandbox Code Playgroud)

回收行动的商店:

global.todoListStore = Reflux.createStore({

    listenables: [TodoActions],
    onCostChange: function(){
        alert('test1');
    }
});
Run Code Online (Sandbox Code Playgroud)

订阅/侦听商店的组件

var CalcApp = React.createClass({
    mixins: [Reflux.listenTo(todoListStore,"onStatusChange")],
    onStatusChange: function() {
        alert('test2');
    },
        getInitialState: function(){
            return{
                      cat1: this.props.cat1
                  };
         },
  render: function() {
    return (// redacted)
  }
});
Run Code Online (Sandbox Code Playgroud)

我能够将第一个组件(CalcRow)连接到它的存储,并触发警报('test1'),但我没有成功使CalcApp监听todoListStore并让它发出警报('test2').

我已经阅读了官方的Reflux文档,但似乎有一些我遗漏的东西,因为CalcApp没有按预期收听todoListStore.

有没有人对我如何做到这一点有任何见解(CalcApp)听反馈商店(todoListStore)?

Mos*_*sho 5

你的商店不排放任何东西.你需要打电话this.trigger:

global.todoListStore = Reflux.createStore({

    listenables: [TodoActions],
    onCostChange: function(){
        this.trigger('test1');
    }
});
Run Code Online (Sandbox Code Playgroud)

文档