For some reason, every time I trigger an action in my react component, the store method associated with the action gets triggers twice. Using the Firefox debugger I noticed that the event emitter seems to be "emitting" the action two times, despite the fact that I am only calling the action once (onClick).
Component
var TodoHead = React.createClass({
addItem: function(e){
var todo = this.refs.TodoInput.getDOMNode().value;
TodoActions.addTodoItem(todo);
// signal that there was a change to the todo object/array
TodoActions.todoItemsChanged();
},
removeItem: function(){
TodoActions.removeItem();
TodoActions.todoItemsChanged();
},
render: function(){
return (
// buttons that triggers the methods above onClick
);
}
});
Reflux store
var todoItems = [];
var API = {
addTodoItem: function(item){
debugger;
if(item != ""){
todoItems.push(item);
}
},
removeTodoItem: function(){
todoItems.pop();
},
}
var TodoStore = Reflux.createStore({
init: function(){
this.listenTo(TodoActions.addTodoItem,API.addTodoItem);
this.listenTo(TodoActions.removeItem,API.removeTodoItem);
},
getTodos: function(){
return todoItems;
},
});
Reflux Actions
var TodoActions = Reflux.createActions([
'addTodoItem',
'removeItem',
'todoItemsChanged'
]);
As you can imagine, this has been a real thorn in my side. What am I doing wrong?
Any answers will be appreciated!!