-
Notifications
You must be signed in to change notification settings - Fork 866
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
Whitelist reducer subproperty only? #277
Comments
right now to achieve said functionality you would need to implement a custom transform, or use a package like https://github.com/edy/redux-persist-transform-filter The reason for this is we want to keep redux-persist as simple as possible and preserve our ability to do type checking with typescript and flow. I think more exploration needs to be done to figure out how to flexibly enable granular dynamic filtration in a performant way. e.g. If you figure out a good solution please let us know. A few possible solutions without creating a new module:
|
@rt2zz thanks for the feedback. I ended up using the redux-persist-transform-filter and followed the advice that the creator gave and ended up with this: const persitingReducers = createFilter(
`purchases.list`
);
persistStore(store, {
storage: AsyncStorage,
transforms: [
persitingReducers
]
}); |
@dwilt it does not work for me. |
This is what i've been doing and it's been working really well for me in my projects. It keeps the UI state separate from the business data and allows for a better organization |
@dwilt @icesyc import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { persistStore, persistCombineReducers } from 'redux-persist';
import { createWhitelistFilter } from 'redux-persist-transform-filter';
import { PersistGate } from 'redux-persist/es/integration/react';
import storage from 'redux-persist/es/storage';
import reducers from './reducers';
import Router from './Router';
const persistConfig = {
storage,
key: 'root',
transforms: [
createWhitelistFilter('router', []),
createWhitelistFilter('form', [])
]
};
const reducer = persistCombineReducers(persistConfig, reducers);
const store = createStore(reducer);
const persistor = persistStore(store);
const App = () => (
<Provider store={store}>
<PersistGate persistor={persistor}>
<Router />
</PersistGate>
</Provider>
);
ReactDOM.render(<App />, document.getElementById('root')); |
Just wanted to mention an alternative to |
I came up with a 9-line solution that uses import { AsyncStorage } from 'react-native';
import { persistReducer } from 'redux-persist';
export function persist(key, whitelist, reducer) {
return persistReducer({
key,
storage: AsyncStorage,
whitelist,
}, reducer);
} You can now whitelist specific fields on any reducer. When creating reducers, you can export the reducer as usual if it doesn't need to persist any keys, but you can use the function defined above to declare which fields to persist, and specify a unique key for the reducer in AsyncStorage. For instance, you can persist entire reducers in your root reducer, e.g.: import { combineReducers } from 'redux';
// Import your persist function from wherever you created it
import { persist } from 'path/to/persist';
// import your reducers, and define your root reducer as usual,
// e.g. in a const named rootReducer
const rootReducer = combineReducers({
activeUser,
configuration,
credentials,
someOtherReducer,
});
// Use your imported `persist` function, specify the key "root", and whitelist any
// entire reducers as desired (here, we're not persisting someOtherReducer)
export default persist(
'root',
[
'activeUser',
'configuration',
'credentials',
],
rootReducer,
); But you can also specify only specific keys at reducers at any depth: // Import your persist function from wherever you created it
import { persist } from 'path/to/persist';
// define your reducer as usual
const initialState = {
keyToIgnore: 'Don\'t persist this',
keyToPersist: 'Persist this',
};
function someOtherReducer(state = initialState, action) {
switch (action.type) {
// etc.
}
}
// Then pass it to your `persist` function before exporting,
// with the key name and whitelist of specific keys to persist
export default persist(
'someOtherReducer',
['keyToPersist'],
someOtherReducer,
); I hope this helps anyone researching this particular issue. I have implemented this with Flow typings in practice. If anyone is interested in that, feel free to reach out. |
For ones who are still searching for updates and appeared here. persist updated his documentation and set up nested configs and combining so you can create multiple configs related to multiple reducers and deep black-white listing |
@hrachocode could you please tell how migrate such nested configs with deep black-white listing in case if nested reducers must operate data from parent reducers? |
Answering the main question - that's possible!
and here is what you can do using my package redux-deep-persist, which can be helpful in creating a redux-persist config. import { getPersistConfig } from 'redux-deep-persist';
const config = getPersistConfig({
key: 'root',
storage: LocalStorage, // whatever storage you use
whitelist: ['purchases.list'], // you can go as deep as you need, using dot notation
rootReducer, // your root reducer must be also passed here
... // any other props from original redux-persist config, omitting the stateReconciler
}); |
If I have an object on my state called
purchases
and it has two properties, an array called 'list' and a boolean called 'syncing':Is there a way I can just whitelist my
list
array like so?It doesn't seem to work.
The text was updated successfully, but these errors were encountered: