3

我正在尝试将 redux-storage 和 redux-saga 中间件用于 redux。每个中间件单独工作都很好,但是当我使用 redux-saga 应用 redux-storage 时,存储不会加载先前的状态。我添加中间件如下:

const combinedReducers = storage.reducer(combineReducers(reducers));
import createEngine from 'redux-storage-engine-reactnativeasyncstorage';
const engine = createEngine('storage-key');
const storageMiddleWare = storage.createMiddleware(engine);
const sagaMiddleWare = createSagaMiddleware(mySagas); 
export const store  = createStore(combinedReducers, applyMiddleware( sagaMiddleWare, storageMiddleWare)); 
 const load = storage.createLoader(engine);
 load(store).then(() => 
    {   
        console.log("Loaded State");
        console.log(store.getState());

    }).done();
4

2 回答 2

1

中间件按照传入的顺序执行applyMiddleware。从存储加载应该在 saga 中间件之前,例如:

const finalCreateStore = compose(
  applyMiddleware(
    storage.createMiddleware(engine),
    createSagaMiddleware(sagas)
  )
)(createStore)
于 2016-03-27T03:04:17.207 回答
0

哦!一堆 'EFFECT_TRIGGERED' 动作立即从 redux-saga 发出。在存储加载之前,这些在 redux-storage 中触发空存储的保存事件。解决方案是在创建 redux-storage 中间件时将 EFFECT_TRIGGERED 添加到黑名单中,如下所示:

const storageMiddleWare = storage.createMiddleware(engine, [= 'EFFECT_TRIGGERED']);
于 2016-03-07T23:56:45.307 回答