【发布时间】:2016-01-19 11:43:30
【问题描述】:
我有一个包含以下组件的 React/Flux 应用程序:
- header.js
- subnav-area.js
- feed-area.js
我也有相应的店铺:
- navbar-store.js(标题)
- subnav-store.js
- feed-store.js
...和行动:
- navbar-actions.js(标题)
- subnav-actions.js
- feed-actions.js
按下子导航上的按钮时,提要和子导航都应更新,设置正确的提要类型并突出显示活动的子导航元素。我成功实现了这个目标,但我也多次启动了 onChange 事件,也使用 header.js 组件,即使它不应该受到操作的影响。
在动作中我这样调用调度员:
AppDispathcer.handleViewAction({
actionType: AppConstants.SUBNAV_SET_ACTIVE,
_id: subnavItemId
})
...当然,取决于目的。我的调度员看起来像这样:
handleViewAction: 函数(动作){
this.dispatch({ 来源:'VIEW_ACTION', 行动:行动 }) }
在商店里我有这样的东西来处理调度员:
dispatcherIndex: AppDispatcher.register(function(payload) {
var action = payload.action;
switch(action.actionType) {
case AppConstants.FILTER_SET_CAT:
FeedStore.selectVisibleFeedItems();
break;
case AppConstants.FEED_SHOW_MORE:
FeedStore.showMoreItems(filterCatName);
break;
}
FeedStore.emitChange();
return true;
})
所有组件都有以下功能(但监听器名称不同):
componentWillMount:function(){
FeedStore.addFeedListener(this._onChange, 'feed-area.js');
},
componentWillUnmount:function() {
FeedStore.removeFeedListener(this._onChange)
},
_onChange: function(){
this.setState(getVisibleFeedItems())
},
现在,当所需的操作发生时,_onChange 会在以下所有组件上启动:标题、子导航区域和提要区域组件。
我从哪里开始寻找错误?我不完全理解调度程序是如何工作的,我应该以某种方式分离处理视图的功能吗?我以为 Flux 会以某种方式监听商店并根据它更新视图,现在它似乎在监听其他内容,我真的不明白是什么。
【问题讨论】:
标签: javascript node.js reactjs reactjs-flux flux