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

时间:2017-02-16 06:25:58

标签: ios json react-native react-native-router-flux cyclic

我们正在构建一个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"
}

3 个答案:

答案 0 :(得分:3)

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

Link

我为lazy做了一个简单的库,它覆盖了JSON.stringify(),允许它处理循环引用而不产生异常。它可以防止您在第三方库中更改有关此限制的任何内容。在代码的引导程序中安装它。

Link 2

答案 1 :(得分:0)

将此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;
    };
};

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

JSON.stringify(circularReference, getCircularReplacer());
// {"otherData":123}

答案 2 :(得分:0)

您正在为REACT NATIVE编写,但是似乎您对textInput的更新值感兴趣时,似乎已经在textInput上使用了onChange而不是onChangeText,这是React Native的正确方法,