redux-persist icon indicating copy to clipboard operation
redux-persist copied to clipboard

Ineffective re-saving of the same data

Open gentlee opened this issue 3 years ago • 0 comments

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.

gentlee avatar Aug 30 '22 15:08 gentlee