2016-10-02 116 views
0

我正在構建我的第一個反應原生應用程序。我習慣於使用Flux MVC模式來構建React應用程序。我(正確地)將Flux集成到我的React Native項目中,但調度程序上的register函數似乎完全沒有響應。React Native + Flux,無響應調度程序

Actions.js

import AppConstants from '../constants'; 
import AppDispatcher from '../dispatcher'; 

export default { 
    setUser (user) { 
     AppDispatcher.dispatch({ 
      actionType: AppConstants.SET_USER, 
      user: user 
     }) 
    } 
} 

Store.js

import AppDispatcher from '../dispatcher'; 
import AppConstants from '../constants'; 
import assign from 'object-assign'; 
import { EventEmitter } from 'events'; 

const CHANGE_EVENT = 'change'; 

const AppStore = assign({}, EventEmitter.prototype, { 

    emitChange() { 
     this.emit(CHANGE_EVENT); 
    }, 

    addChangeListener (callback) { 
     this.on(CHANGE_EVENT, callback); 
    }, 

    removeChangeListener (callback) { 
     this.removeListener(CHANGE_EVENT, callback); 
    }, 

    dispatcherIndex: register(function (action) { 
     console.log(action); // <-- No response 
    }) 

}); 

export default AppStore; 

Dispatcher.js

import { Dispatcher } from 'flux'; 
module.exports = new Dispatcher(); 

我甚至嘗試重寫Dispatcher.js文件,以便dispatchregister函數是硬編碼的。我收到dispatch()的響應,但從未從register()得到響應。

Dispatcher2.js

import { Dispatcher } from 'flux'; 

const flux = new Dispatcher(); 

export function register (callback) { 
    console.log('event!'); 
    return flux.register(callback); 
} 

export function dispatch (action) { 
    console.log(action); 
    flux.dispatch(action); 
} 

回答

0

通過這個文檔。我不知道你的代碼,但這樣我們也可以使用flux。 http://fluxxor.com/。這非常簡單易用。如果這對您的問題有幫助,請接受答案。

相關問題