【问题标题】:react Dispatcher.dispatch in action, not dispatching payload to store在行动中反应 Dispatcher.dispatch,而不是将有效负载分派到存储
【发布时间】:2016-12-03 16:34:46
【问题描述】:

我在“action”中设置了有效负载,目的是将 api 数据发送到商店。但是在 Dispatcher.register 中不会触发 switch case。

助焊剂版本:“助焊剂”:“^2.1.1”,

1)动作文件:(注意:已确认receiveAllServices是使用调试器触发的)

"use strict"

var Dispatcher = require('../dispatcher/appDispatcher');
// var requestActions = require('./requestActions');
var ActionTypes = require('../constants/actionTypes');

var ResponseActions = {

    receivedAllServices: function(all_services){

        console.log('response received');
        debugger;

        Dispatcher.dispatch({
            actionType: ActionTypes.RECEIVED_ALL_SERVICES,
            services: all_servicess
        });
    }



};

module.exports = ResponseActions;

2) 存储:(注意:存储操作中的调试器不会触发)

Dispatcher.register(function(action){
    switch(action.actionType){
        case ActionTypes.RECEIVED_ALL_SERVICES:

            debugger;

            // AuthorStore.emitChange();
            break;
    }
});

3) 调度程序文件:

var Dispatcher = require('flux').Dispatcher;

module.exports = new Dispatcher();

4) actionTypes.js 文件

"use strict"

var keyMirror = require('fbjs/lib/keyMirror');

module.exports = keyMirror({
    RECEIVED_ALL_SERVICES: null, 
});

【问题讨论】:

    标签: reactjs flux reactjs-flux


    【解决方案1】:

    嗯,很奇怪。一切看起来都是正确的。你能检查store.jsaction.js中的Dispatcher是同一个对象吗?

    请参阅下面具有相同操作/商店的简短示例。一切正常(按run 并查看控制台输出)。

    var appDispatcher = new Flux.Dispatcher();
    
    // This code should be our action
    function receivedAllServices() {
      appDispatcher.dispatch({
        actionType: 'RECEIVED_ALL_SERVICES',
        services: ['serv1','serv2'],
      });
      console.log('Done');
    }
    
    
    // This code should be our store
    appDispatcher.register(action => {
      console.log('action', action);
      switch(action.actionType){
        case 'RECEIVED_ALL_SERVICES':
          //debugger;
          // AuthorStore.emitChange();
          console.log('AuthorStore.emitChange');
          break;
      }
    });
    
    // Call the action
    receivedAllServices();
    <script src="https://cdnjs.cloudflare.com/ajax/libs/flux/2.1.1/Flux.js"></script>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-19
      • 2019-01-16
      相关资源
      最近更新 更多