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

Persist state of redux #23

Closed
AleXzpm opened this issue Apr 24, 2021 · 9 comments
Closed

Persist state of redux #23

AleXzpm opened this issue Apr 24, 2021 · 9 comments

Comments

@AleXzpm
Copy link

AleXzpm commented Apr 24, 2021

Using redux-injectors makes it imposible to persist the state because the injectorsEnhancer regularly replaces the rootReducer with a new one - and only the initial one of those is persisted. Am I wrong? How can we persist the state of redux?
image
image

@siarheipashkevich
Copy link

@AleXzpm hi, did you find the solution?

@BenLorantfy
Copy link
Collaborator

redux-persist has a piece of documentation that implies one needs to call persistReducer every time they call replaceReducer. Since we call replaceReducer internally within this library, it's not possible to use redux-persist currently.

We could probably add a mechanism for allowing this. Maybe some kind of hook onReplaceReducer or something. I'll try to find some time to look into this.

@siarheipashkevich
Copy link

siarheipashkevich commented May 3, 2021

@BenLorantfy thank you, and could you please investigate how we can use immer with your library?

@AleXzpm
Copy link
Author

AleXzpm commented May 9, 2021

@siarheipashkevich no I did not, other then giving up on Injectors. Hopefully @BenLorantfy can create some hook for this.

@BenLorantfy
Copy link
Collaborator

@siarheipashkevich immer should work fine with this library... were you running into any issues?

@BenLorantfy
Copy link
Collaborator

@AleXzpm I thought about this some more and I don't think this requires any changes in redux-injectors. Something like this should work:

const createReducer = (injectedReducers) => {
  const combinedReducer = combineReducers(injectedReducers);
  const persistedReducer = persistReducer(persistConfig, combinedReducer);
  return persistedReducer;
}

The key is to move the persistReducer call into createReducer. Lmk if that doesn't work for you.

@Pixel-Jack
Copy link

Pixel-Jack commented Jun 11, 2021

It works for me thank you @BenLorantfy 🙏

@hellofantastic
Copy link

hellofantastic commented Feb 3, 2022

@BenLorantfy @Pixel-Jack
Should that last solution work all the same if its dynamic injected reducers like in react-boilerplate-cra-template

import { combineReducers } from '@reduxjs/toolkit';

import { InjectedReducersType } from 'utils/types/injector-typings';

/**
 * Merges the main reducer with the router state and dynamically injected reducers
 */
export function createReducer(injectedReducers: InjectedReducersType = {}) {
  // Initially we don't have any injectedReducers, so returning identity function to avoid the error
  if (Object.keys(injectedReducers).length === 0) {
    return state => state;
  } else {
    return combineReducers({
      ...injectedReducers,
    });
  }
}

as

export function createReducer(injectedReducers: InjectedReducersType = {}) {
  // Initially we don't have any injectedReducers, so returning identity function to avoid the error
  if (Object.keys(injectedReducers).length === 0) {
    return state => state;
  } else {
       const combinedReducers = combineReducers({ ...injectedReducers, });
       const persistedReducer = persistReducer(persistConfig, combinedReducer);
    return persistedReducer;
  }
}

and then in configureStore leave it as

const store = configureStore({
    reducer: createReducer(),
    ...etc
});

@hellofantastic
Copy link

EDIT, nope, need to refactor saga reducer wiring, store config, to include reducers/sagas statically for persisting and rehydrating for react-boilerplate-cra-template

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

5 participants