现在,我正在尝试将redux持久性集成到我的redux-saga中,以react native方式进行。我已经在CreateStore中尝试了一些代码。我的应用程序可以运行,但是减速器总是在重新加载应用程序后重置。
这是我的代码
// 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 };
}
// 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>
);
}
}
有人能解决这个问题吗?我希望减速器在重新加载应用程序之前保留先前的数据。
谢谢大家
答案 0 :(得分:0)
好吧,我终于可以在redux saga中使用reduxpersist。 这是我的最后一个代码
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 };
}