Add transformer to transform Date.

This commit is contained in:
Ceda EI 2020-03-21 20:22:08 +05:30
parent 83d0310bdc
commit dedf976c95
1 changed files with 10 additions and 1 deletions

View File

@ -1,11 +1,20 @@
import { createStore } from "redux"; import { createStore } from "redux";
import { persistStore, persistReducer } from "redux-persist"; import { persistStore, persistReducer, createTransform } from "redux-persist";
import AsyncStorage from "@react-native-community/async-storage"; import AsyncStorage from "@react-native-community/async-storage";
import rootReducer from "./reducers"; import rootReducer from "./reducers";
function decode(toRehydrate) {
return JSON.parse(toRehydrate, (_, value) => {
if (typeof value === "string" && value.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/))
return new Date(value);
return value;
});
}
const persistConfig = { const persistConfig = {
key: "root", key: "root",
storage: AsyncStorage, storage: AsyncStorage,
transforms: [createTransform(JSON.stringify, decode)],
}; };
const persistedReducer = persistReducer(persistConfig, rootReducer); const persistedReducer = persistReducer(persistConfig, rootReducer);