Coder Social home page Coder Social logo

Comments (9)

BenLorantfy avatar BenLorantfy commented on April 28, 2024 6

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.

from redux-injectors.

BenLorantfy avatar BenLorantfy commented on April 28, 2024 2

@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.

from redux-injectors.

Pixel-Jack avatar Pixel-Jack commented on April 28, 2024 2

It works for me thank you @BenLorantfy 🙏

from redux-injectors.

siarheipashkevich avatar siarheipashkevich commented on April 28, 2024

@AleXzpm hi, did you find the solution?

from redux-injectors.

siarheipashkevich avatar siarheipashkevich commented on April 28, 2024

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

from redux-injectors.

AleXzpm avatar AleXzpm commented on April 28, 2024

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

from redux-injectors.

BenLorantfy avatar BenLorantfy commented on April 28, 2024

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

from redux-injectors.

hellofantastic avatar hellofantastic commented on April 28, 2024

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

from redux-injectors.

hellofantastic avatar hellofantastic commented on April 28, 2024

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

from redux-injectors.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.