Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for v5 of Redux-Persist #11

Open
alshdotme opened this issue Dec 12, 2017 · 13 comments
Open

Update for v5 of Redux-Persist #11

alshdotme opened this issue Dec 12, 2017 · 13 comments

Comments

@alshdotme
Copy link

alshdotme commented Dec 12, 2017

I was wondering if this library was going to be/is compatible with redux-persist v5? I've downgraded to v4 for now.

@ChenLi0830
Copy link

+1 for supporting v5.

@gabceb
Copy link
Owner

gabceb commented Dec 21, 2017

I’m taking a look into v5

@gediminastub
Copy link

How's the progress going? Thank You for such a wonderful library!

@hcyildirim
Copy link

Is there any improvement? 😞

@nikosolihin
Copy link

+1 for v5

@leonardosul
Copy link

leonardosul commented May 17, 2018

I'm not sure if this needs to up be updated to work with V5 of redux persist. I have been using redux-persist v5.9.1 with the latest version of redux-persist-transform-expire successfully.

My config looks like this:

import ReactDOM from 'react-dom';
import { applyMiddleware, createStore, compose } from "redux";
import thunk from "redux-thunk";
import promise from "redux-promise-middleware";
import reducer from "./reducers";
import { Provider } from 'react-redux';
import { BrowserRouter as Router } from 'react-router-dom';
import {persistStore, persistReducer} from "redux-persist";
import { PersistGate } from 'redux-persist/integration/react';
import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2';
import createExpirationTransform from "redux-persist-transform-expire";
import localForage from "localforage";
import config from "./config";
import App from './App';

// Redux persist config.
const expireTransform = createExpirationTransform({
  expireKey: 'persistExpiresAt',
  defaultState: {}
});
const persistConfig = {
  key: 'root',
  storage: localForage,
  stateReconciler: autoMergeLevel2,
  whitelist: [
    "crypto"
  ],
  transforms: [expireTransform]
};

// Create our store
const middlewares = [];
let middleware;
middlewares.push(thunk);
middlewares.push(promise());
middleware = compose(applyMiddleware(...middlewares));

// Persist the store.
const persistedReducer = persistReducer(persistConfig, reducer);
const store = createStore(persistedReducer, middleware);
const persistor = persistStore(store);

// Where to render the react app.
const root = document.getElementById('root');

ReactDOM.render(
  <Provider store={store}>
    <PersistGate loading={null} persistor={persistor}>
      <Router>
        <App />
      </Router>
    </PersistGate>
  </Provider>,
  root
);

The creator of redux-persist has stated that there is no reason that this library should stop working because of the V5 release: rt2zz/redux-persist#508 (comment)

If you are still having trouble let me know what is broken and I will try to submit a patch.

@gabceb
Copy link
Owner

gabceb commented May 17, 2018

Hi everyone! Apologies for the radio silence but I've been a bit busy on the work side. If anyone wants to put a PR together I'll be happy to merge. Otherwise, I'll try to pick this up next week. If you don't hear from me feel free to keep pinging me so I don't forget (but will try not to)

@adamtay
Copy link

adamtay commented May 29, 2018

+1 for v5 compatibility

@pekq
Copy link

pekq commented Jul 12, 2018

@gabceb Sorry to bug you on this, but have you had any progress?
Nvm, it's pretty straightforward to make your own transformers :)

@leonardosul
Copy link

@pekq @adamtay This transform is V5 compatible. Have a look at my comment above. Using a similar config you can use this with V5. If you are still having issues after using my configuration let me know the specifics in this issue thread 👍

@appjitsu
Copy link

@leonardosul can you please share your reducer that has persistExpiresAt in it?

What I am trying to accomplish is to treat my auth reducer, which has a jwt token, as a session. I'd like to expire the "session" after say 30 minutes, detect that change and redirect the user to the login view.

@leonardosul
Copy link

Hey @appjitsu, this is what my reducer looked like for this particular use case:

import moment from "moment";
import {
  START_FETCH_OPTIONS,
  FETCH_OPTIONS_SUCCESS,
  FETCH_OPTIONS_FAILURE
} from "../actions/CryptoActions";

export default function reducer(state={
  isLoading: false,
  optionsList: {
    coinmarketcap_usd: [],
  },
  persistExpiresAt: moment()
    .add(5, 'm')
    .format(),
}, action) {

  switch (action.type) {
    case START_FETCH_OPTIONS: {
      return {
        ...state,
        isLoading: true,
      }
    }
    case FETCH_OPTIONS_SUCCESS: {
      return {
        ...state,
        optionsList: {
          ...state.optionsList,
          [action.cryptoOptionsId]: action.optionsList
        },
        isLoading: false
      }
    }
    case FETCH_OPTIONS_FAILURE: {
      return {
        ...state,
        isLoading: false,
      }
    }
    default:
      return state;
  }
}

I'm using moment.js to set the data to expire in 5 mins from "now". If you use a similar pattern but add 30 mins from now like this .add(30, 'm') you should be able to get what you need out of this transformer.

@kamranahmedse
Copy link

I ended up making/publishing my own transformer for my use case. Have a look if it helps anyone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests