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

stateReconciler deep merge example #491

Open
umairfarooq44 opened this issue Oct 18, 2017 · 4 comments
Open

stateReconciler deep merge example #491

umairfarooq44 opened this issue Oct 18, 2017 · 4 comments

Comments

@umairfarooq44
Copy link

I have following state
`{ bluerain: {
intl: {
locale: 'en',
rtl: false,
messages: {
en:{},
ur:{},
ch:{},
}
}

}`
I have used redux-persist-transform-filter to ignore "messages" object while storing state in localstorage, initially messages are stored in initial state of redux store but due to shallow merge mesages in my initial store are not merged with that of localstorage state. Any suggestions so that my messages object will not stored in localstorage but I get messages from initial state in my store after autoRehydrate

@rt2zz
Copy link
Owner

rt2zz commented Oct 18, 2017

options:

  1. do not nest messages, bring it up either to the top level of the reducer, or as its own reducer
  2. implement a custom REHYDRATE handler in your reducer. In there you can do whatever custom deep merge you like and then autoRehydrate will simply ignore that substate when it sees the state has been modified during rehydration.
  3. propose a PR to make state reconcilers pluggable. I think we want to go in this direction, because there are too many possible use cases to cover all with one reconciler. Some useful reconcilers that we can ship with are hardSet, shallowMerge, deepMerge, mergeLevel2. Not 100% on the naming but thats the idea.

@umairfarooq44
Copy link
Author

Can you please elaborate how to add reducer because after adding reducer new object with reducer name is created in the state and it have state from the local storage in that reducer and state updation of "intl" object in the above state is not affected by my reducer.

@veharrison
Copy link

Isn't there a way we can write our own reconcilers or mention merge levels in configuration.

@BARTlab
Copy link

BARTlab commented Feb 21, 2024

import { cloneDeep, isObject, merge, omit } from 'lodash';

stateReconciler: (
  inboundState: Record<string, {}>,
  originalState: Record<string, {}>,
  reducedState: Record<string, {}>,
): Record<string, {}> => {
  const newState = cloneDeep(omit(reducedState, ['_persist']));

  return isObject(inboundState) ? merge({}, newState, inboundState) : newState;
},

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

4 participants