【问题标题】:React Flux - can not dispatch in the middle of a dispatchReact Flux - 不能在调度过程中调度
【发布时间】:2016-01-29 19:11:18
【问题描述】:

我有一个带有列表的下拉菜单。单击列表时,它会显示在页面另一侧的表格中。

每当创建一个新列表时,我都希望像单击它一样显示它(如下面的代码所示)。但是,为了在表格中显示它,我需要调用ListAssignmentsStoreActions.loadListAssignments(list);

handleClick: function() {
    var list = this.props.data;
    ListAssignmentsStoreActions.loadListAssignments(list);
},

//invoked after initial rendering
componentDidMount: function() {
    var loadAssignmentsForList = this.props.loadAssignmentsForList;
    console.log("Name " + this.props.data.name);
    if(loadAssignmentsForList){
        this.handleClick();
    }
}

问题是我得到了:

 error = Error: Invariant Violation: Dispatch.dispatch(...): Cannot dispatch in the middle of a dispatch. at invariant

除了调用一个动作,我不知道如何显示列表。

【问题讨论】:

  • 没有代码很难说。这是一个调度错误,例如我们可能需要您的操作文件...

标签: javascript reactjs reactjs-flux flux


【解决方案1】:

我相信你弄错了 Flux。

当您在下拉列表中选择一个列表时,您会发送一个 Action,例如 ListSelectedAction,它有一个保存列表 ID 的属性。

你应该有一个 Store,比如 ListStore,它在调度程序上注册一个监听器/回调,监听 ListSelectedAction。 ListStore 使用包含在ListSelectedAction 中的列表ID 更新其属性之一,例如currentList,并发出一个事件,例如LIST_CHANGED

Table 组件侦听LIST_CHANGED 事件(不是操作),并在接收到它时从ListStore 读取currentList id。 (ListStore 作为道具传递给 Table)

总之, 下拉菜单 -> ListSelectedAction -> ListStore -> LIST_CHANGED -> 表格

与新按钮相同

新建按钮 -> NewListAction -> ListStore -> List_CHANGED -> 表格

【讨论】:

    猜你喜欢
    • 2014-12-22
    • 2016-04-22
    • 2015-06-29
    • 2017-02-16
    • 2017-02-10
    • 2017-04-12
    • 2015-09-06
    • 2016-06-23
    • 2015-09-13
    相关资源
    最近更新 更多