React Native JSON.stringify无法序列化循环结构

Vic*_*aso 8 json cyclic ios react-native react-native-router-flux

我们正在构建一个RN应用程序(RN0.37),我们遇到了一个问题,当应用程序运行时,我们得到一个" TypeError: JSON.stringify cannot serialize cyclic structures".

API响应中没有任何相关性发生变化,最近问题消失了,但是在擦除/重建时(由不相关的问题触发)重新出现.

我怀疑是使用了几个包:" react-native-router-flux"和" react-native-permissions",但我无法在应用程序中找到任何相关内容.

目前我对"react-native-router-flux"的怀疑主要基于这篇文章:https://github.com/aksonov/react-native-router-flux/issues/363

我对"反应本机权限"的怀疑主要是基于这样一个事实,即在这个项目中包含这个包的时机是可疑的,并且似乎与这个错误的表现相吻合 - 尽管我无法证明绝对肯定.

我唯一的另一个线索是,JSON.stringify错误似乎总是在一个警告列表之前.他们都读到"这个合成事件是出于性能原因重复使用的.如果你看到这个,你就是在一个已发布/无效的合成事件上访问该属性.这被设置为null.如果你必须保留原始合成事件,使用event.persist()https://facebook.github.io/react/docs/events.html#event-pooling以获取更多信息." 列表如下(始终按相同顺序):nativeEvent,type,target,currentTarget,eventPhase,bubbles,cancelable,timeStamp,defaultPrevented,isTrusted和touchHistory.

以下是我的package.json:

"dependencies": {
  "blueimp-md5": "2.5.0",
  "moment": "2.16.0",
  "phone-formatter": "0.0.2",
  "react": "15.3.2",
  "react-native": "0.37.0",
  "react-native-asset-library-to-base64": "1.0.1",
  "react-native-aws3": "0.0.3",
  "react-native-button": "1.7.1",
  "react-native-cached-image": "1.2.2",
  "react-native-camera-kit": "4.0.1",
  "react-native-camera-roll-picker": "1.1.9",
  "react-native-contacts": "0.5.2",
  "react-native-fbsdk": "0.4.0",
  "react-native-fetch-blob": "0.10.0",
  "react-native-fs": "2.0.1-rc.2",
  "react-native-geocoder": "0.4.5",
  "react-native-image-crop-picker": "0.10.5",
  "react-native-image-resizer": "0.0.12",
  "react-native-nav": "1.1.4",
  "react-native-permissions": "0.2.5",
  "react-native-photo-view": "1.2.0",
  "react-native-router-flux": "3.37.0",
  "react-native-stripe": "1.2.1",
  "react-native-swipe-list-view": "0.3.1",
  "react-redux": "4.4.6",
  "redux": "3.6.0",
  "redux-storage": "4.1.1",
  "redux-storage-engine-reactnativeasyncstorage": "1.0.2",
  "underscore": "1.8.3"
}
Run Code Online (Sandbox Code Playgroud)

Bil*_*nko 7

将此getCircularReplacer函数作为第二个参数传入JSON.stringify()将修复此错误:

const getCircularReplacer = () => {
    const seen = new WeakSet();
    return (key, value) => {
    if (typeof value === "object" && value !== null) {
        if (seen.has(value)) {
            return;
        }
        seen.add(value);
    }
    return value;
    };
};
Run Code Online (Sandbox Code Playgroud)

然后您可以按如下方式使用它:

JSON.stringify(circularReference, getCircularReplacer());
// {"otherData":123}
Run Code Online (Sandbox Code Playgroud)


kba*_*sis 5

JSON.stringify 无法处理引用自身或自身部分的 JSON 对象。

关联

我为懒惰创建了一个简单的库,它覆盖了 JSON.stringify() 以允许它处理循环引用而不会产生异常。它使您无法更改 3rd 方库中有关此限制的任何内容。在代码的引导程序中安装它。

链接 2