redux-persist/createPersistoid:错误序列化状态 TypeError:将循环结构转换为 JSON

Sof*_*ory 3 react-native redux google-cloud-firestore redux-persist

我正在尝试将 redux-persist 集成到我的 react-native 项目中。目的是在应用程序重新启动之间保留 redux 存储的数据,以便用户无需在每次启动应用程序时登录。另外,我想将以前查看的数据存储在本地存储上,以避免每次重新启动应用程序时重新查询所有数据。

我按照下面的代码添加了 redux 持久支持:

import { AsyncStorage } from 'react-native';
import { createStore, applyMiddleware } from 'redux';
//import { createLogger } from 'redux-logger';
import { persistStore, persistReducer } from 'redux-persist';
import rootReducer from '../reducers/index';
import ReduxThunk from 'redux-thunk';
import { createTransform } from 'redux-persist';
import JSOG from 'jsog';

export const JSOGTransform = createTransform(
    (inboundState, key) => JSOG.encode(inboundState),
    (outboundState, key) => JSOG.decode(outboundState),
)

const persistConfig = {
    // Root
    key: 'root',
    // Storage Method (React Native)
    storage: AsyncStorage,
    //transforms: [JSOGTransform]
    // Whitelist (Save Specific Reducers)
    // whitelist: [
    //   'authReducer',
    // ],
    // // Blacklist (Don't Save Specific Reducers)
    // blacklist: [
    //   'counterReducer',
    // ],
};

// Middleware: Redux Persist Persisted Reducer
const persistedReducer = persistReducer(persistConfig, rootReducer);

// Redux: Store
const store = createStore(
    persistedReducer,
    applyMiddleware(ReduxThunk),
);

// Middleware: Redux Persist Persister
let persistor = persistStore(store);
// Exports
export {
  store,
  persistor,
};
Run Code Online (Sandbox Code Playgroud)

如果我不添加 jsog 并使用 persistConfig 中的transforms: [JSOGTransform]行,我会收到此错误:

redux-persist/createPersistoid:错误序列化状态 TypeError:将循环结构转换为 JSON

如果我取消注释 persistConfig 中的“转换”行(根据此处的建议:https : //github.com/rt2zz/redux-persist/issues/735),则会收到此错误:

HostObject::set 中的异常:<unknown>

我只是将Firestore数据库中返回的“用户”对象保存在我的 redux-store 中。如果没有 redux-persist,就没有问题,但是添加了 persist 我遇到了这个问题。

成功登录firestore(使用密码/电子邮件身份验证)后返回的用户对象中会存在什么类型的循环问题?

为什么 JSOG 不能按照上面链接中的建议工作?我如何解决这个问题的任何替代方法?

PS 不仅是从 firestore 返回的用户导致了这些错误,而且从 firestore 返回的任何数据似乎都无法持久化。

我感谢任何帮助!干杯...

Sof*_*ory 7

我想我解决了这个问题。相反JSOG我安装分层,并用于我的终极版-persist的变换。

工作转换和persistConfig 看起来像这样:

export const transformCircular = createTransform(
    (inboundState, key) => Flatted.stringify(inboundState),
    (outboundState, key) => Flatted.parse(outboundState),
)

const persistConfig = {
    key: 'root',
    storage: AsyncStorage,
    stateReconciler: autoMergeLevel2,
    transforms: [transformCircular]
};
Run Code Online (Sandbox Code Playgroud)