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文件,以便dispatch
和register
函数是硬编码的。我收到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);
}