简体   繁体   中英

Not triggering React event correctly

I have a Reflux store which seems to cause an error when triggering an action:

var taskStore = Reflux.createStore({
  listenables: [taskActions],
  init: function(){
    this.tasks = [];
    this.index = {};
    this.trigger(this.tasks);
  },
  onLoad: function(arr){
    var idx = {}
    arr.forEach(function(ele){
      idx[ele.id] = ele
    });
    this.tasks = arr;
    this.index = idx;
    this.trigger(arr);
  }
});

When the action load is called and the onLoad method triggers, it fails on the this.trigger method with Chrome saying the error is Uncaught TypeError: Cannot read property 'apply' of undefined

The call stack eventually gets me to the Reflux listen method:

listen: function(callback, bindContext) {
    bindContext = bindContext || this;
    var eventHandler = function(args) {
        callback.apply(bindContext, args);
    }, me = this;
    this.emitter.addListener(this.eventLabel, eventHandler);
    return function() {
        me.emitter.removeListener(me.eventLabel, eventHandler);
    };
},

It's the callback in callback.apply, but it looks like that callback is undefined for some reason .

I'm clearly doing something wrong to trigger such a deep error, but I don't know what. Can someone tell me what it is that I'm doing wrong in my store?

The code that calls the store calls it like this:

var App = React.createClass({
    mixins: [
        Reflux.connect(taskStore,"tasks"),
        Reflux.connect(taskIndex, "index"),
    ],
...

I've had the same problem. In my case it was my fault when declaring the component:

global.FilmList = React.createClass({
    mixins: [Reflux.connect(filmListStore, "list")],
    componentDidMount: function() {
        this.listenTo(filmListStore, this.eventChange);
    },
    render: function() {

I had set both mixins and this.listenTo() method. I've removed the this.listenTo() and this solved the problem

Just spent an hour trying to solve this issue myself.

In my case, the method i told Reflux.ListenTo to trigger, didn't exist.

So i had

Reflux.listenTo('FooStore', 'onUserDeleted')

and i didnt have onUserDeleted: function() { } in the store.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM