【问题标题】:'loading' remains true when loading data with 'useQuery' using 'apolloClient' in react-native在 react-native 中使用 'apolloClient' 使用 'useQuery' 加载数据时,'loading' 保持为真
【发布时间】:2022-06-23 21:03:40
【问题描述】:

我正在尝试在 React Native 中使用 Apollo Client useQuery 获取数据,但 loading 在真实状态下挂起并且无法获取 data

下面是实际代码。

App.js

import React from 'react';

import { SafeAreaView, Text, View } from 'react-native';

import { ApolloClient, ApolloProvider, InMemoryCache, gql, useQuery } from '@apollo/client';

const EXCHANGE_RATES = gql`
    query GetExchangeRates {
        rates(currency: "USD") {
            currency
            rate
        }
    }
`;

const TestScreen = () => {
    const { loading, error, data } = useQuery(EXCHANGE_RATES);

    console.log(loading, error, data); // Stop at "true undefined undefined" ????

    if (loading) return <Text>Loading...</Text>;
    if (error) return <Text>Error :(</Text>;

    return data.rates.map(({ currency, rate }) => (
        <View key={currency}>
            <Text>
                {currency}: {rate}
            </Text>
        </View>
    ));
};

const apolloClient = new ApolloClient({
    // this uri is the uri provided by the apolloClient official documentation.
    // https://www.apollographql.com/docs/react/get-started#3-connect-your-client-to-react
    uri: 'https://48p1r2roz4.sse.codesandbox.io',
    cache: new InMemoryCache(),
});

const App = () => {
    return (
        <ApolloProvider client={apolloClient}>
            <SafeAreaView>
                <TestScreen />
            </SafeAreaView>
        </ApolloProvider>
    );
};

export default App;

package.json

{
    ...,
    "dependencies": {
        "@apollo/client": "^3.5.10",
        "graphql": "^16.4.0",
        "react": "17.0.2",
        "react-native": "0.68.1",
        "react-native-flipper-apollo-devtools": "^0.0.2"
    },
    "devDependencies": {
        "@babel/core": "^7.12.9",
        "@babel/runtime": "^7.12.5",
        "@graphql-codegen/cli": "^2.6.2",
        "@graphql-codegen/fragment-matcher": "^3.2.1",
        "@graphql-codegen/typed-document-node": "^2.2.8",
        "@graphql-codegen/typescript": "^2.4.8",
        "@graphql-codegen/typescript-apollo-client-helpers": "^2.1.15",
        "@graphql-codegen/typescript-operations": "^2.3.5",
        "@react-native-community/eslint-config": "^2.0.0",
        "@types/jest": "^26.0.23",
        "@types/react-native": "^0.67.3",
        "@types/react-test-renderer": "^17.0.1",
        "@typescript-eslint/eslint-plugin": "^5.17.0",
        "@typescript-eslint/parser": "^5.17.0",
        "babel-jest": "^26.6.3",
        "babel-plugin-module-resolver": "^4.1.0",
        "eslint": "^7.32.0",
        "eslint-config-prettier": "^8.5.0",
        "eslint-config-standard-with-typescript": "^21.0.1",
        "eslint-plugin-import": "^2.26.0",
        "eslint-plugin-node": "^11.1.0",
        "eslint-plugin-promise": "^6.0.0",
        "eslint-plugin-react": "^7.29.4",
        "eslint-plugin-react-hooks": "^4.5.0",
        "eslint-plugin-react-native": "^4.0.0",
        "husky": "^7.0.4",
        "jest": "^26.6.3",
        "lint-staged": "^12.4.1",
        "metro-react-native-babel-preset": "^0.67.0",
        "react-native-flipper": "^0.144.0",
        "react-test-renderer": "17.0.2",
        "typescript": "^4.4.4"
    },
    "resolutions": {
        "@types/react": "^17"
    },
    "jest": {
        "preset": "react-native",
        "moduleFileExtensions": [
            "ts",
            "tsx",
            "js",
            "jsx",
            "json",
            "node"
        ]
    }
}

预期结果:

运行后useQuery()

loading = true, error = undefined, data = undefined
                    ↓
loading = false, error = undefined, data = { ... }

实际结果:

运行后useQuery()

loading = true, error = undefined, data = undefined (Stuck at 'loading = true' ????)

我不知道问题是什么。任何帮助将不胜感激。

【问题讨论】:

  • 您找到解决方案了吗?我也有同样的问题。我也在使用 Expo 及其 Metro 来测试本地应用程序。
  • 请看下面我的回答,新的alpha版本解决了这个问题。

标签: react-native apollo-client


【解决方案1】:

不幸的是,这是一个已知错误,发生在 apollo 客户端 3.6 中。最后一个正常工作的版本是 3.5.10。之后,useQuery 和 useLazyQuery 停止工作(卡在加载中),但仅在移动设备/模拟器上。网页版仍然可以正常工作。 尝试降级到 3.5.10。

https://github.com/apollographql/apollo-client/issues/9689

编辑:新的 alpha 版本解决了这种情况 (3.7.0-beta.3)。

试试看:npm i @apollo/client@beta

【讨论】:

    【解决方案2】:

    版本 3.6 使用来自 React 18 的 useSyncExternalStore API 实现 useQuery。由于 expo 当前支持 React 17,您可以通过将 @apollo/client 降级到版本 3.5.4graphql 到版本 ^16.3.0 来解决这个问题目前。您可以找到更多信息here

    【讨论】:

      【解决方案3】:

      升级到新的 alpha 版本 (3.7.0-beta.3) 来自@Dominik Myszkowski 解决了我的问题,降级到 3.5.4 没有。

      【讨论】:

      • 现在已在次要版本中修复。您也可以将@apollo/client 更改为 3.6.8 版本,我可以确认它正在运行。
      猜你喜欢
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      • 1970-01-01
      相关资源
      最近更新 更多