rt2zz / redux-persist

persist and rehydrate a redux store
MIT License
12.95k stars 865 forks source link

Reducer still reset after reload the app #1033

Open pisangGoreng opened 5 years ago

pisangGoreng commented 5 years ago

Hi Everyone,

Right now I'm trying to integrate redux persist to my redux-saga in react native. I'm already trying some code in CreateStore. My app can run, but the reducer always reset after reloading the app.

This is my code

// 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>
    );
  }
}

Does anyone have a clue for solving this issue or I'm wrong to implement Redux persist?? I want the reducer to keep the previous data before reloading the app.

Thanks, Everyone

pisangGoreng commented 5 years ago

I finally can use redux persist with redux saga. This is my last code

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 };
}
alburdette619 commented 5 years ago

I'm seeing this issue right now but with rn-thunk and no real changes to my redux-persist setup

mkomeily commented 5 years ago

hi guys, same problem. any other option than redux-sega ? does it going to be solved ?

ShaileshPrajapati-BTC commented 5 years ago

same issue in RN