React Native + Flux,无响应调度程序

React Native + Flux, unresponsive dispatcher

本文关键字:响应 调度程序 Flux Native React      更新时间:2023-09-26

我正在构建我的第一个原生响应应用程序。我习惯使用Flux MVC模式构建React应用程序。我已经(正确地)将Flux集成到我的React Native项目中,但是dispatcher上的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文件,以便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);
}

通读本文档。我不知道你的代码,但这样我们也可以使用flux。http://fluxxor.com/。这是非常简单和容易使用。如果这对你的问题有帮助,请接受这个答案。

你从哪里得到商店的代码?无论如何,当其他属性都是纯函数时,dispatchIndex被作为方法调用是有原因的吗?

冒着给你的代码没有帮助的风险,这里是我如何根据react flux文档来使用香草flux的存储:

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

希望这是有用的,而不仅仅是我告诉你放弃你的代码为一些不相关的