rt2zz / redux-persist

persist and rehydrate a redux store
MIT License
12.91k stars 862 forks source link

Ineffective re-saving of the same data #1408

Open gentlee opened 1 year ago

gentlee commented 1 year ago

Lets say we have usersReducer that stores only current user:

export type UsersState = {
    currentUserId: string;
    profiles: Record<string, UserProfile>;
};

export const usersReducer = persistReducer({
    key: 'users',
    throttle: 5000,
    whitelist: ['currentUserId', 'profiles'],
    transforms: [
        createTransform(
            (subState: UsersState['profiles'], _, state: UsersState) => {
                const currentUserId = state.currentUserId;
                return currentUserId ? {
                    [currentUserId]: subState[currentUserId],
                } : {};
            },
            null,
            { whitelist: ['profiles'] },
        ),
    ],
}, reducer);

So it should call Storage.set only when current user changes. But no, it calls It every time when profiles state changes.

Is there a way to compare prev and new state before saving? Similar to mapStateToProps re-rendering.