zewish / redux-remember

Saves and loads your redux state from a key-value store of your choice
MIT License
82 stars 5 forks source link

Add key argument to serialize and unserialize invocations #3

Closed MatiasArriola closed 3 years ago

MatiasArriola commented 3 years ago

Hello,

First of all, thanks for writing this library!

I came here looking for a redux-persist replacement after hitting some critical unresolved issues in my react-native apps. I'm relieved the move to redux-remember was relatively easy.

The only thing I was missing is some way to omit certain keys per reducer. I figured out I could do that by passing a custom serialize. But there was no way I could tell which slice I'm serializing from inside the serializer, hence this patch.

Here some code that illustrates this use-case:

const serializationOmissions = {
  auth: ['forgotPasswordCode'],
  home: ['loading'],
};

const serialize = (data: object, key = '') => {
  const result = omit(data, serializationOmissions[key]);
  return JSON.stringify(result);
};

const enhancer = rememberEnhancer(
      someStorage,
      ['auth', 'home', 'anyOtherSlice'],
      { serialize }
),

Just wanted to contribute back the changes I needed for my projects, hope you find it valuable.

Cheers!

zewish commented 3 years ago

Hey @MatiasArriola. Thanks for the PR and sorry for answering so late, but I've been quite busy lately. At first glance seems a reasonable proposal. I'll have a look and get this merged and released today :)

coveralls commented 3 years ago

Pull Request Test Coverage Report for Build 1270378095


Totals Coverage Status
Change from base Build 1087060535: 0.0%
Covered Lines: 64
Relevant Lines: 64

💛 - Coveralls
zewish commented 3 years ago

@MatiasArriola, I made the extra key parameter in the serialize and unserialize functions required, this way we can guarantee that your serialize/unserialize function always has a proper key to check against.

The latest version 3.0.1 is now released on npm, this version includes the extra key parameter you needed.