首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何实现redux持久化与redux saga?

如何实现redux持久化与redux saga?
EN

Stack Overflow用户
提问于 2019-05-14 00:08:36
回答 1查看 2.9K关注 0票数 0

现在我正在尝试在react原生中将redux persist集成到我的redux-saga中。我已经在尝试用CreateStore编写一些代码了。我的应用程序可以运行,但在重新加载应用程序后,减速器总是会重置。

这是我的代码

代码语言:javascript
复制
// CreateStore.js
import { applyMiddleware, compose } from 'redux'
import createSagaMiddleware from 'redux-saga'
import { persistStore, persistCombineReducers } from 'redux-persist';
import { AsyncStorage } from 'react-native';
import Reactotron from "reactotron-react-native";

const config = {
  key: 'root',
  storage: AsyncStorage,
};

// creates the store
export default (rootReducer, rootSaga) => {
  /* ------------- Redux Configuration ------------- */
  const middleware = []
  const enhancers = []

  /* ------------- Saga Middleware ------------- */
  const sagaMiddleware = createSagaMiddleware()
  middleware.push(sagaMiddleware)

  /* ------------- Assemble Middleware ------------- */
  enhancers.push(applyMiddleware(...middleware))


  const reducers = persistCombineReducers(config, rootReducer);
  const persistConfig = { enhancers };

  const store = Reactotron.createStore(rootReducer, compose(...enhancers));
  persistStore(store);

  // kick off root saga
  sagaMiddleware.run(rootSaga)

  return { store };
}
代码语言:javascript
复制
// app.js
import React, { Component } from "react";
import { Provider } from "react-redux";
import Reactotron from "reactotron-react-native";

import createStore from '../src/Redux'
import PrimaryNav from "../src/navigations/AppNavigations";


export default class App extends Component {
  render() {
    const { store } = createStore()
    console.log = Reactotron.log
    console.disableYellowBox = true;

    return (
      <Provider store={store}>
        <PrimaryNav />
      </Provider>
    );
  }
}

有谁有解决这个问题的线索吗?我希望reducer在重新加载应用程序之前保留以前的数据。

谢谢大家

EN

回答 1

Stack Overflow用户

发布于 2019-05-15 11:51:09

好吧,我终于可以使用redux持久化和redux saga了。这是我的最后一个代码

代码语言:javascript
复制
import { applyMiddleware, compose } from 'redux'
import createSagaMiddleware from 'redux-saga'
import Reactotron from "reactotron-react-native";
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';

const persistConfig = {
  key: 'root',
  storage,
}

// creates the store
export default (rootReducer, rootSaga) => {
  /* ------------- Redux Configuration ------------- */
  const middleware = []
  const enhancers = []

  /* ------------- Saga Middleware ------------- */
  const sagaMiddleware = createSagaMiddleware()
  middleware.push(sagaMiddleware)

  /* ------------- Assemble Middleware ------------- */
  enhancers.push(applyMiddleware(...middleware))

  const persistedReducer = persistReducer(persistConfig, rootReducer)
  const store = Reactotron.createStore(persistedReducer, compose(...enhancers));
  const persistor = persistStore(store)

  // kick off root saga
  sagaMiddleware.run(rootSaga)

  return { store, persistor };
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56116124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档