Skip to content

Commit

Permalink
Improve redux-toolkit support - no need for combineReducers
Browse files Browse the repository at this point in the history
  • Loading branch information
zewish committed Dec 10, 2022
1 parent 2c3e494 commit b7f5b92
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
4 changes: 2 additions & 2 deletions index.d.ts
@@ -1,4 +1,4 @@
import { Action, AnyAction, Reducer } from 'redux';
import { Action, AnyAction, Reducer, ReducersMapObject } from 'redux';
declare const REMEMBER_REHYDRATED = "@@REMEMBER_REHYDRATED";
declare const REMEMBER_PERSISTED = "@@REMEMBER_PERSISTED";
type SerializeFunction = (data: any, key: string) => any;
Expand All @@ -14,6 +14,6 @@ type Options = {
persistThrottle: number;
persistWholeStore: boolean;
};
declare const rememberReducer: <S = any, A extends Action<any> = AnyAction>(reducers: Reducer<S, A>) => Reducer<S, A>;
declare const rememberReducer: <S = any, A extends Action<any> = AnyAction>(reducer: Reducer<S, A> | ReducersMapObject<S, A>) => Reducer<S, A>;
declare const rememberEnhancer: (driver: Driver, rememberedKeys: string[], { prefix, serialize, unserialize, persistThrottle, persistWholeStore }?: Partial<Options>) => any;
export { rememberReducer, rememberEnhancer, REMEMBER_REHYDRATED, REMEMBER_PERSISTED };
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "redux-remember",
"version": "3.1.4",
"version": "3.1.5",
"description": "Saves and loads your redux state from a key-value store of your choice",
"main": "lib/index.js",
"module": "es/index.js",
Expand Down
5 changes: 4 additions & 1 deletion rollup.config.mjs
Expand Up @@ -12,7 +12,10 @@ const config = {
sourcemap: true,
format: 'umd',
esModule: false,
strict: false
strict: false,
globals: {
'redux': 'Redux'
}
},
external: ['redux'],
plugins: [
Expand Down
14 changes: 10 additions & 4 deletions src/index.ts
Expand Up @@ -8,11 +8,13 @@ import {
StoreEnhancer,
Reducer,
Store,
StoreCreator
StoreCreator,
ReducersMapObject,
combineReducers
} from 'redux';

const rememberReducer = <S = any, A extends Action = AnyAction>(
reducers: Reducer<S, A>
reducer: Reducer<S, A> | ReducersMapObject<S, A>
): Reducer<S, A> => {
const data: any = {
state: {}
Expand All @@ -26,9 +28,13 @@ const rememberReducer = <S = any, A extends Action = AnyAction>(
data.state = { ...state };
}

const rootReducer = typeof reducer === 'function'
? reducer
: combineReducers(reducer) as Reducer<S, A>;

switch (action.type) {
case REMEMBER_REHYDRATED:
data.state = reducers(
data.state = rootReducer(
{
...data.state,
...(action.payload || {})
Expand All @@ -39,7 +45,7 @@ const rememberReducer = <S = any, A extends Action = AnyAction>(
return data.state;

default:
return reducers(
return rootReducer(
state,
action
);
Expand Down

0 comments on commit b7f5b92

Please sign in to comment.