Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clarify docs on parameters of functions inside createTransform() #1446

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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