gpt4 book ai didi

javascript - 回流 Action 触发两次

转载 作者:行者123 更新时间:2023-11-30 00:32:05 25 4
gpt4 key购买 nike

出于某种原因,每次我在我的 React 组件中触发一个 Action 时,与该 Action 关联的存储方法都会触发两次。使用 Firefox 调试器,我注意到事件发射器似乎“发射”了两次 Action ,尽管事实上我只调用了一次 Action (onClick)。

组件

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

回流商店

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

反流行动

    var TodoActions = Reflux.createActions([        'addTodoItem',        'removeItem',        'todoItemsChanged'    ]);

如您所想,这一直是我心头的痛。我做错了什么?

任何答案将不胜感激!

最佳答案

您不需要 TodoActions.todoItemsChanged()

它的工作原理是您只需调用 TodoActions.addTodoItem(todo)

因此,没有 todoItemsChanged 操作。

Store 监听 Action。Component 监听 Store。组件调用操作。

所以,Action->Store->Component->Action等等。

store 监听 Action 并执行操作,然后触发更改:

var TodoStore = Reflux.createStore({
init: function(){
this.listenTo(TodoActions.addTodoItem,this.addTodoItem);
this.listenTo(TodoActions.removeItem,this.removeTodoItem);
},
getInitialState() {
this.todos = []; // or whatever
return this.todos;
},
addTodoItem(todo) {
API.addTodoItem(todo);
// There are no hard and fast rules here,
// you can do this however you want
this.update([todo].concat(this.todos));
},
removeTodoItem() {
// Similar to the above
},
// The docs do use this method
// but you can call this.trigger from the liteners
update(todos) {
this.todos = todos;
this.trigger(todos);
},
});

您可能需要调整您的 API 以适应这种格式。 Store 应该存储实际的待办事项并调用 API。上面的方法更新本地状态而不检查 API 是否成功。我调用我的 api 并使用这样的回调(Api 在幕后使用 superagent):

// Actual code I pulled for a project I'm working on
onAddNote(id, note) {
Api.createNote(id, note, (err, res) => {
let lead = JSON.parse(res.text).lead;
this.updateLead(lead);
})
},

组件监听 store 的变化(并调用你已经拥有的 Actions):

var TodoStore = require('/path/to/TodoStore');
var Reflux = require('reflux');

var TodoHead = React.createClass({
mixins: [Reflux.connect(TodoStore, 'todos')],
// **insert the rest of your component**
// this will add the current value of
// of the todos onto the state, you
// access it at this.state.todos

每次商店调用 this.trigger(this.todos) 时,组件的状态都会更新,这要归功于 Mixin。

还有其他方法可以连接到商店,请参阅 The Reflux Docs .

关于javascript - 回流 Action 触发两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29064743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com