Skip to content

Commit

Permalink
clarify docs on parameters of functions inside createTransform()
Browse files Browse the repository at this point in the history
  • Loading branch information
azerum committed Jun 16, 2023
1 parent d8b01a0 commit 324ebf1
Showing 1 changed file with 38 additions and 10 deletions.
48 changes: 38 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,18 +254,46 @@ Below is a Transform that successfully persists a Set property, which simply con
import { createTransform } from 'redux-persist';

const SetTransform = createTransform(
// transform state on its way to being serialized and persisted.
(inboundState, key) => {
// convert mySet to an Array.
return { ...inboundState, mySet: [...inboundState.mySet] };
// Transform state on its way to being serialized and persisted.
// This function is called for every field of the state
//
// `value` - the value of the current field
// `key` - the name of the current field
// `fullState` - full Redux state object (with all fields)
//
// This function returns updated value of the currently transfomed field
(value, key, fullState) => {
// If current field is `mySet`, replace it with array

if (key !== 'mySet') {
return value;
}

return [...inboundState.value];
},
// transform state being rehydrated
(outboundState, key) => {
// convert mySet back to a Set.
return { ...outboundState, mySet: new Set(outboundState.mySet) };

// Transform state being rehydrated
// Parameters and return value have the same meaning as in the function above
(value, key, fullState) => {
// If current field is `mySet`, convert it back to set

if (ket !== 'mySet') {
return value;
}

return new Set(value);
},
// define which reducers this transform gets called for.
{ whitelist: ['someReducer'] }

{
// Whitelist - the list of fields which will be transformed
// Blacklist - the list of fields that won't be transformed

// The `if` checks in functions above could be unnecessary, for example,
// if we written only `['mySet']` in the `whitelist`

whitelist: ['thoseFieldsWillBeTransformed', 'mySet'],
blacklist: ['thisFieldWillNotBeTransformed']
}
);

export default SetTransform;
Expand Down

0 comments on commit 324ebf1

Please sign in to comment.