Redux Persist – Create filter

I’m new to React Redux. Can you tell me how to manually write the below saveUserFilter function? I don’t want to use redux-persist-transform-filter anymore as it’s using lodash.set, which is vulnerable to Prototype Pollution.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const saveUserFilter = createFilter('user', [
'username',
'displayName',
'timeZone',
])
const persistConfig = {
key: KEY,
storage,
whitelist: [
'user',
'timeZones',
],
transforms: [ saveUserFilter ],
}
</code>
<code>const saveUserFilter = createFilter('user', [ 'username', 'displayName', 'timeZone', ]) const persistConfig = { key: KEY, storage, whitelist: [ 'user', 'timeZones', ], transforms: [ saveUserFilter ], } </code>
const saveUserFilter = createFilter('user', [
  'username',
  'displayName',
  'timeZone',
])

const persistConfig = {
  key: KEY,
  storage,
  whitelist: [
    'user',
    'timeZones',
  ],
  transforms: [ saveUserFilter ],
}

Glad to see you! I know how you feel about lodash.set which is used in redux-persist-transform-filter can be vulnerable to prototype pollution, especially when considering the cases when object assignment uses Object.create(). But don’t worry because there is an easy method that can help you get rid of this method when saving user filters by making use of the following.

First, let’s define the saveUserFilter function. This function will manually filter the user state to include only the properties you need: username, displayName, and timeZone.

Here’s how you can do it:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { createTransform } from 'redux-persist';
const saveUserFilter = createTransform(
// Transform state before it's serialized and persisted.
(inboundState, key) => {
if (key === 'user') {
// Only include specific properties in the persisted state.
const { username, displayName, timeZone } = inboundState;
return { username, displayName, timeZone };
}
return inboundState;
},
// Transform state being rehydrated.
(outboundState, key) => {
return outboundState;
},
// Specify which reducer this transform applies to.
{ whitelist: ['user'] }
);
const persistConfig = {
key: 'root',
storage,
whitelist: ['user', 'timeZones'],
transforms: [saveUserFilter],
};
export default persistConfig;
</code>
<code>import { createTransform } from 'redux-persist'; const saveUserFilter = createTransform( // Transform state before it's serialized and persisted. (inboundState, key) => { if (key === 'user') { // Only include specific properties in the persisted state. const { username, displayName, timeZone } = inboundState; return { username, displayName, timeZone }; } return inboundState; }, // Transform state being rehydrated. (outboundState, key) => { return outboundState; }, // Specify which reducer this transform applies to. { whitelist: ['user'] } ); const persistConfig = { key: 'root', storage, whitelist: ['user', 'timeZones'], transforms: [saveUserFilter], }; export default persistConfig; </code>
import { createTransform } from 'redux-persist';

const saveUserFilter = createTransform(
  // Transform state before it's serialized and persisted.
  (inboundState, key) => {
    if (key === 'user') {
      // Only include specific properties in the persisted state.
      const { username, displayName, timeZone } = inboundState;
      return { username, displayName, timeZone };
    }
    return inboundState;
  },
  // Transform state being rehydrated.
  (outboundState, key) => {
    return outboundState;
  },
  // Specify which reducer this transform applies to.
  { whitelist: ['user'] }
);

const persistConfig = {
  key: 'root',
  storage,
  whitelist: ['user', 'timeZones'],
  transforms: [saveUserFilter],
};

export default persistConfig;

Explanation:

  1. Create Transform: We’re using redux-persist‘s createTransform to define a custom transformation.
  2. Inbound State: This part filters the state before it’s saved. We’re only keeping the properties (username, displayName, timeZone) we need from the user state.
  3. Outbound State: This just returns the state as it is when it’s rehydrated.
  4. Whitelist: We specify the user reducer in the whitelist so that this transform is applied only to it.

For more details on creating transforms with redux-persist, you can check out the official documentation.

Feel free to reach out if you have any more questions or need further help. Good luck with your project!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật