我正在构建我的第一个react-native应用程序。我习惯于使用Flux MVC模式构建React应用程序。我(正确地)将Flux集成到我的React Native项目中,但调度程序上的register
函数似乎完全没有响应。
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);
}
答案 0 :(得分:0)
浏览此文档。我不知道你的代码,但这样我们也可以使用flux。 http://fluxxor.com/。这非常简单易用。如果这对您的问题有帮助,请接受答案。
答案 1 :(得分:0)
您从哪里获得商店的代码?无论如何,当其余属性都是纯函数时,是否有理由将dispatchIndex用作方法?
冒着给您带来无济于事的代码的风险,这是我如何根据反应通量文档中的香草通量学习如何使用存储的方法:
import Immutable from 'immutable'
import {ReduceStore} from 'flux/utils'
import myActionTypes from './../data/myThing/myActionTypes'
import dispatcher from '../data/dispatcher'
import AppConstant from '../data/myThing/AppConstant'
class myStore extends ReduceStore{
constructor(){
super(dispatcher);
}
getInitialState(){
return Immutable.OrderedMap({});
}
reduce(state,action){
switch(action.actionType){
case AppConstant.SET_USER:
return state.set({//data you want to set})
case AppConstant.OTHER_THING:
//additional logic
希望这是有用的,而不仅仅是我告诉你放弃不相关的代码。